How do I use strcasestr()?

32,471

Solution 1

As specified in the corresponding manpage, since strcasestr is a nonstandard extension you must #define _GNU_SOURCE before the #include <string.h> before any #include (other files may already include <string.h>, thanks @Cubbi for pointing out this potential problem); this can also easily be accomplished by specifying -D_GNU_SOURCE on the compiler command line.

Solution 2

You must add:

#define _GNU_SOURCE

before the string.h include, since the function is non-standard.

Share:
32,471
Jack
Author by

Jack

Computer lover. :-) -- I'm not a native speaker of the english language. So, if you find any mistake what I have written, you are free to fix for me or tell me on. :)

Updated on July 05, 2022

Comments

  • Jack
    Jack almost 2 years

    I #include <string.h> but when I call strcasestr(src, search); I get the following error message implicit declaration of function ‘strcasestr’. how to I compile: gcc-4.6 -Wall -lsqlite3 -lunac -Werror -O2 -o foo.out foo.c how to fix this? Thanks in advance.

  • Jack
    Jack about 12 years
    I added too the prototype function:#include <string.h> #define _GNU_SOURCE char *strcasestr(const char *haystack, const char *needle); and works fine now. Thanks very much.
  • Matthew Flaschen
    Matthew Flaschen about 12 years
    @Jack, you do not need to (and should not) add the prototype. However, the define needs to be before the include.
  • Cubbi
    Cubbi almost 12 years
    Caveat: make sure that #define is the first line in the file. If you #include <stdio.h>, then #define _GNU_SOURCE, and then #include <string.h>, you won't get strcasestr because stdio.h already included string.h. The safe choice is compiling with -D_GNU_SOURCE.
  • Hefaz
    Hefaz about 5 years
    I added that line as well, but still does not work. any help?
  • Matteo Italia
    Matteo Italia about 5 years
    @Hefaz What compiler/platform are you using?
  • Hefaz
    Hefaz about 5 years
    CodeBlocks in Windows. I solved the problem by defining that function. Thank you.