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

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
回复
dylan13536
帖子: 6
注册时间: 2014-01-12 15:05
系统: ubuntu

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

#1

帖子 dylan13536 » 2014-03-09 20:13

大牛,新手求帮助!


/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 }
头像
maplebeats
帖子: 378
注册时间: 2011-02-16 1:17

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

#2

帖子 maplebeats » 2014-03-09 20:32

没有定义,定义一个就行了啊
My blog : OOXX
dylan13536
帖子: 6
注册时间: 2014-01-12 15:05
系统: ubuntu

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

#3

帖子 dylan13536 » 2014-03-09 21:06

不会定义啊,新手~~
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

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

#4

帖子 YeLee » 2014-03-09 21:09

我这边没问题啊,用msvc就可以了。 :em01
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
dylan13536
帖子: 6
注册时间: 2014-01-12 15:05
系统: ubuntu

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

#5

帖子 dylan13536 » 2014-03-09 21:16

YeLee 写了:我这边没问题啊,用msvc就可以了。 :em01
Ubuntu 可以安装MSVC吗?刚用Ubuntu很多都不懂
dylan13536
帖子: 6
注册时间: 2014-01-12 15:05
系统: ubuntu

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

#6

帖子 dylan13536 » 2014-03-09 21:18

Ubuntu 可以安装MSVC吗,刚用Ubuntu有很多不懂的地方,求耐心指导
头像
huangbster
帖子: 187
注册时间: 2012-10-29 11:35
系统: UBUNTU

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

#7

帖子 huangbster » 2014-03-10 14:11

man strnlen 看看是否符合需求。
dylan13536
帖子: 6
注册时间: 2014-01-12 15:05
系统: ubuntu

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

#8

帖子 dylan13536 » 2014-03-10 15:18

huangbster 写了:man strnlen 看看是否符合需求。
main strlen可以用,据说不安全什么的,strnlen_s就不可以用了
头像
huangbster
帖子: 187
注册时间: 2012-10-29 11:35
系统: UBUNTU

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

#9

帖子 huangbster » 2014-03-10 16:38

dylan13536 写了:
huangbster 写了:man strnlen 看看是否符合需求。
main strlen可以用,据说不安全什么的,strnlen_s就不可以用了
没这个道理,安全的。如果多线程操作同一个字符串,那么就加锁吧。
头像
cjxgm
帖子: 1952
注册时间: 2010-04-23 20:40
系统: Arch Linux
来自: 浙江·杭州
联系:

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

#10

帖子 cjxgm » 2014-03-10 21:00

strlen_s, strncpy_s 和 strncat_s 函数都是微软的扩展,linux 没有的,也不需要
Clanjor Prods. | Develop for Developers. (C++, Lua) | 作曲编曲 | 实时渲染引擎
dylan13536
帖子: 6
注册时间: 2014-01-12 15:05
系统: ubuntu

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

#11

帖子 dylan13536 » 2014-03-14 16:26

cjxgm 写了:strlen_s, strncpy_s 和 strncat_s 函数都是微软的扩展,linux 没有的,也不需要
谢谢啦,难怪ubuntu用不了
头像
VampirEM木法沙
帖子: 151
注册时间: 2013-10-29 19:36
系统: ubuntu12.04

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

#12

帖子 VampirEM木法沙 » 2014-03-14 20:03

cjxgm 写了:strlen_s, strncpy_s 和 strncat_s 函数都是微软的扩展,linux 没有的,也不需要
赞一个!
之前编程遇到TLS相关的操作,也困惑了好一会。
在地狱中仰望天堂!
三十年河东,三十年河西,莫欺少年穷!
别在可以吃苦的时候贪图安逸!
回复