简体   繁体   中英

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. 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. 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.

The same functionality may be available there under the name stristr or _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:

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

A pointer to the null-terminated string being searched.

pszSrch Type: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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