strtok_s is undefined on os x

I'm trying to use the C11 function strtok_s, which is supposed to be defined in <string.h>, but clang is giving me a linking error when I try to use it. Compiling this program:

#include <string.h>

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

Gives me this error:

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)

I'm using clang 8.1.0 on OS X 10.12.6. What am I doing wrong?

2 answers

  • answered 2018-04-14 14:24 melpomene

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

    As all bounds-checked functions, strtok_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including string.h.

  • answered 2018-04-14 14:24 Stargateur

    strtok_s is optionnel in C11, honestly only windows implement it, use strtok_r on Posix system...