在os x上未定义strtok_s

时间:2018-04-14 14:24:06

标签: c string macos clang tr24731

我试图使用C11函数strtok_s,它应该在<string.h>中定义,但是当我尝试使用它时,clang会给我一个链接错误。编译这个程序:

#include <string.h>

int main() {
    char * buffer;
    char * state;
    rsize_t strmax = 0;
    char * fpl = strtok_s(buffer, &strmax, "\n", &state);
}

给我这个错误:

repro.c:7:15: warning: implicit declaration of function 'strtok_s' is invalid in
  C99 [-Wimplicit-function-declaration]
    char * fpl = strtok_s(buffer, &strmax, "\n", &state);
                 ^
repro.c:7:9: warning: incompatible integer to pointer conversion initializing
  'char *' with an expression of type 'int' [-Wint-conversion]
    char * fpl = strtok_s(buffer, &strmax, "\n", &state);
           ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 warnings generated.
Undefined symbols for architecture x86_64:
  "_strtok_s", referenced from:
  _main in repro-3dcfc9.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我在OS X 10.12.6上使用clang 8.1.0。我做错了什么?

2 个答案:

答案 0 :(得分:4)

strtok_s在C11中是可选的;老实说,只有Windows实现它。在POSIX系统上使用strtok_r

答案 1 :(得分:3)

http://en.cppreference.com/w/c/string/byte/strtok

  

作为所有边界检查函数,只有在实现定义strtok_s并且用户将__STDC_LIB_EXT1__定义为整数常量__STDC_WANT_LIB_EXT1__时,才保证1可用。在包括string.h之前。