简体   繁体   English

C 中的 strcasestr 如何工作。 不断收到错误外部符号

[英]How does strcasestr in C work. Keep getting Error external symbol

I have defined _GNU_SOURCE but when i try to put strcasestr in my function it just says error LNK2019: unresolved external symbol _strcasestr referenced in function.我已经定义了 _GNU_SOURCE,但是当我尝试将 strcasestr 放入我的函数时,它只是说错误 LNK2019:函数中引用的未解析的外部符号 _strcasestr。 Do i need to import a specific library somehow or do something else?我是否需要以某种方式导入特定的库或做其他事情? I have also tried defining:我也试过定义:

//char *strcasestr(const char *haystack, const char *needle);
#define _GNU_SOURCE        
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

The way i use it我使用它的方式

if ((strcasestr(str1,str2)) != NULL)
    {
      //code
    }

Any other way to compare strings without case sensitivity?还有其他方法可以比较不区分大小写的字符串吗?

strcasestr is a non standard function. strcasestr是一个非标准函数。 What library are you linking with your project?您将什么库与您的项目链接? If you use MingW in Windows, this function may not be available as your program is linked with the Microsoft C runtime library.如果您在 Windows 中使用 MingW,则此功能可能不可用,因为您的程序与 Microsoft C 运行时库相关联。

The same functionality may be available there under the name stristr or _stristr .相同的功能可能在名称stristr_stristr下可用。

Otherwise, you may have to write your own version.否则,您可能必须编写自己的版本。

Here is a very simplistic one:这是一个非常简单的:

#include <stdlib.h>
#include <ctype.h>

char *strcasestr(const char *str, const char *pattern) {
    size_t i;

    if (!*pattern)
        return (char*)str;

    for (; *str; str++) {
        if (toupper((unsigned char)*str) == toupper((unsigned char)*pattern)) {
            for (i = 1;; i++) {
                if (!pattern[i])
                    return (char*)str;
                if (toupper((unsigned char)str[i]) != toupper((unsigned char)pattern[i]))
                    break;
            }
        }
    }
    return NULL;
}

Can you just use:你能不能只用:

#include <shlwapi.h>
...
#define strcasestr StrStrIA 

From microsoft's crazy documentation pages:来自微软疯狂的文档页面:

"StrStrIA function: "StrStrIA 函数:

Finds the first occurrence of a substring within a string.查找字符串中子字符串的第一次出现。 The comparison is not case-sensitive."比较不区分大小写。”

PCSTR StrStrIA(
  PCSTR pszFirst,
  PCSTR pszSrch
);

Where:在哪里:

"Parameters: “参数:

pszFirst Type: PTSTR pszFirst 类型:PTSTR

A pointer to the null-terminated string being searched.指向正在搜索的以空字符结尾的字符串的指针。

pszSrch Type: PCTSTR pszSrch 类型:PCTSTR

A pointer to the substring to search for."指向要搜索的子字符串的指针。”

Remember to add shlawpi.lib to your project settings else it's not going to link.请记住将 shlawpi.lib 添加到您的项目设置中,否则它不会链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM