2016-09-15 168 views
1

我正在使用Visual Studio 2015,并且正在尝试使用strtok_r。出于某种原因,编译器不能识别它。Strtok_r无法解析的外部符号

这里是我的代码:http://linux.die.net/man/3/strtok_r

的功能是弃用:

#include <string.h> 
#include <stdlib.h> 
#include <assert.h> 

char** str_split(char* a_str, const char a_delim, int * argc) 
{ 
    ... some other code 
    if (result) 
    { 
    size_t idx = 0; 
    char* saveptr = a_str; 
    char* token = strtok_r(a_str, delim, &saveptr); 
    //char * token; 
    while (token) 
    { 
     assert(idx < count); 
     *(result + idx++) = strdup(token); 
     token = strtok_r(0, delim, &saveptr); 
    } 
    assert(idx == count - 1); 
    *(result + idx) = 0; 
    } 

return result; 

我有这个文件已经下?还是我犯了一个愚蠢的错误?提前谢谢你们。

+3

MS不完全POSIX兼容。如果使用MS工具进行编程,则需要阅读[MS文档](https://msdn.microsoft.com/en-us/library/2c8d19sb.aspx),而不是Linux文档。 – kaylum

+1

实际上,POSIX-ish系统上的strtok_r()与Windows系统上的strtok_s()相当。这种对等并不总是有效,但在这种特定情况下,它确实如此。 –

回答

7

Linux手册页不提供Windows平台的文档。你需要使用以下功能之一:

strtok_s,_strtok_s_l,wcstok_s,_wcstok_s_l,_mbstok_s,_mbstok_s_l

[0] https://msdn.microsoft.com/en-us/library/2c8d19sb.aspx

+0

当然,这个问题是_compiler_ visual-studio-2015而不是操作系统。 – chux

+1

Visual Studio只在WIndows平台上,所以我认为它暗示 –

+0

@chux你让编译器和标准库混淆了。 strtok是标准库的一部分,而不是编译器。在这种情况下,更准确地说** C实现**,它将涵盖编译器,运行时和标准库。 – jforberg