分页: 1 / 1

编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-09 20:13
dylan13536
大牛,新手求帮助!


/tmp/cctZxVzL.o:在函数‘main’中:
6_4.c:(.text+0xfa):对‘strnlen_s’未定义的引用
6_4.c:(.text+0x128):对‘strnlen_s’未定义的引用

编写的程序如下:

代码: 全选

// Program 6.4 Joining strings 
  2 #define __STDC_WANT_LIB_EXT1__ 1            // Make optional cersions of functions available
  3 #include <string.h>                       // Header for string functions
  4 #include <stdio.h>
  5 
  6 int main(void)
  7 {
  8     char preamble[] = "The jole is:\n\n";
  9     char str[][40] = {
 10         "My dog hasn\'t gor any nose.\n","How does your dog smell then?\n","My dog smells horrible.\n"};
 11 
 12     unsigned int strCount = sizeof(str)/sizeof(str[0]);
 13 
 14     // Find the total length of all the strings in str
 15     unsigned int length = 0 ;
 16     for(unsigned int i = 1;i < strCount ; ++i)
 17         length += strnlen_s(str[i],sizeof(str[i]));
 18 
 19     // Create array to hold all strings combined
 20 
 21     char joke[length + strnlen_s(preamble,sizeof(preamble))+1];
 22 
 23     if(strncpy_s(joke,sizeof(joke),preamble,sizeof(preamble)))
 24     {
 25         printf("Error copying preamble to joke.\n");
 26         return 1;
 27     }
 28 
 29     // Concatenate strings in joke
 30     for(unsigned int i=0;i<strCount ; ++i)
 31     {
 32         if(strncat_s(joke,sizeof(joke),str[i],sizeof(str[i])))
 33         {
 34             printf("Error copuing string str[%u].",i);
 35             return 2;
 36         }
 37     }
 38     printf("%s",joke);
 39     return 0;
 40 }

Re: 编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-09 20:32
maplebeats
没有定义,定义一个就行了啊

Re: 编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-09 21:06
dylan13536
不会定义啊,新手~~

Re: 编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-09 21:09
YeLee
我这边没问题啊,用msvc就可以了。 :em01

Re: 编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-09 21:16
dylan13536
YeLee 写了:我这边没问题啊,用msvc就可以了。 :em01
Ubuntu 可以安装MSVC吗?刚用Ubuntu很多都不懂

Re: 编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-09 21:18
dylan13536
Ubuntu 可以安装MSVC吗,刚用Ubuntu有很多不懂的地方,求耐心指导

Re: 编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-10 14:11
huangbster
man strnlen 看看是否符合需求。

Re: 编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-10 15:18
dylan13536
huangbster 写了:man strnlen 看看是否符合需求。
main strlen可以用,据说不安全什么的,strnlen_s就不可以用了

Re: 编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-10 16:38
huangbster
dylan13536 写了:
huangbster 写了:man strnlen 看看是否符合需求。
main strlen可以用,据说不安全什么的,strnlen_s就不可以用了
没这个道理,安全的。如果多线程操作同一个字符串,那么就加锁吧。

Re: 编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-10 21:00
cjxgm
strlen_s, strncpy_s 和 strncat_s 函数都是微软的扩展,linux 没有的,也不需要

Re: 编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-14 16:26
dylan13536
cjxgm 写了:strlen_s, strncpy_s 和 strncat_s 函数都是微软的扩展,linux 没有的,也不需要
谢谢啦,难怪ubuntu用不了

Re: 编写C程序的时候出现“对strnlen_s未定义的引用,怎么解决?

发表于 : 2014-03-14 20:03
VampirEM木法沙
cjxgm 写了:strlen_s, strncpy_s 和 strncat_s 函数都是微软的扩展,linux 没有的,也不需要
赞一个!
之前编程遇到TLS相关的操作,也困惑了好一会。