未命名的引用`strnlen_s',strncpy_s',strncat_s'

Dom*_*omi 5 c

    length += strnlen_s(str[i],sizeof(str[i]));

//create array to hold all strings combined

char joke[length + strnlen_s(preamble, sizeof(preamble)) + 1];

if(strncpy_s(joke, sizeof(joke), preamble, sizeof(preamble)))
{
    printf("Error copying  preamble to joke.\n");
    return 1;
}

//Concatenate strings in joke

for(unsigned int i = 0; i < strCount; ++i)
{
    if(strncat_s(joke, sizeof(joke), str[i], sizeof(str[i])))
    {
Run Code Online (Sandbox Code Playgroud)
joiningstring.c:32:3: warning: implicit declaration of function ‘strnlen_s’ [-Wimplicit-function-declaration]
joiningstring.c:38:2: warning: implicit declaration of function ‘strncpy_s’ [-Wimplicit-function-declaration]
joiningstring.c:48:3: warning: implicit declaration of function ‘strncat_s’ [-Wimplicit-function-declaration]
/tmp/ccBnGxvX.o: In function `main':
joiningstring.c:(.text+0x163): undefined reference to `strnlen_s'
joiningstring.c:(.text+0x188): undefined reference to `strnlen_s'
joiningstring.c:(.text+0x1fd): undefined reference to `strncpy_s'
joiningstring.c:(.text+0x251): undefined reference to `strncat_s'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

Syl*_*sne 6

strlen_s,strncpy_sstrncat_s功能是微软扩展标准C库.它们在string.h标题中定义,并且是自动链接的库的一部分.

因此,由于该函数似乎未定义(您得到implicit declaration of function错误),并且未找到(由于undefined reference链接器的错误),我要说您要么尝试在非Microsoft系统上编译此代码(其中情况下,我建议使用替代功能strlen,strncpy,strncat),或者忘记了包括并要求编译器不包括默认库(那么你应该修复代码和编译器调用).

  • 实际上,它们在TR 24731中有记录,现在是ISO/IEC 9899:2011的附录K(可选),即C2011标准.但是,它们并没有在微软之外广泛使用.有关详细信息,请参阅[您是否使用TR 24731'安全'功能](http://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions). (3认同)