How do I use strcasestr()?
Solution 1
As specified in the corresponding manpage, since strcasestr
is a nonstandard extension you must #define _GNU_SOURCE
before the before any #include <string.h>
#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.
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, 2022Comments
-
Jack almost 2 years
I
#include <string.h>
but when I callstrcasestr(src, search);
I get the following error messageimplicit 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 about 12 yearsI 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 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 almost 12 yearsCaveat: 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 about 5 yearsI added that line as well, but still does not work. any help?
-
Matteo Italia about 5 years@Hefaz What compiler/platform are you using?
-
Hefaz about 5 yearsCodeBlocks in Windows. I solved the problem by defining that function. Thank you.