我如何使用strcasestr()?

Jac*_*ack 17 c string gcc

#include <string.h>但是当我打电话时,strcasestr(src, search);我收到以下错误消息implicit declaration of function ‘strcasestr’.我该如何编译:gcc-4.6 -Wall -lsqlite3 -lunac -Werror -O2 -o foo.out foo.c如何解决这个问题?提前致谢.

Mat*_*lia 28

相应联机帮助页中所述,由于strcasestr您是必须的非标准扩展名#define _GNU_SOURCE 之前 #include <string.h>在任何之前#include(其他文件可能已经包含<string.h>,感谢@Cubbi指出这个潜在的问题); 这也可以通过-D_GNU_SOURCE在编译器命令行上指定来轻松完成.

  • 警告:确保#define是文件中的第一行.如果你#include <stdio.h>`,然后`#define _GNU_SOURCE`,然后`#include <string.h>`,你就不会得到strcasestr,因为stdio.h已经包含了string.h.安全的选择是使用-D_GNU_SOURCE进行编译. (4认同)

Mat*_*hen 7

你必须添加:

#define _GNU_SOURCE
Run Code Online (Sandbox Code Playgroud)

string.hinclude 之前,因为函数是非标准的.