码迷,mamicode.com
首页 > 系统相关 > 详细

linux下运行strlwr函数出错:ld returned 1 exit status

时间:2014-10-15 02:03:29      阅读:678      评论:0      收藏:0      [点我收藏+]

标签:   c语言   linux   gcc   

运行strlwr函数时报错,源程序如下:

#include<stdio.h>
#include<string.h>
void main()
{
 char s[10]={"CHINA"};
 printf("%s\n",strlwr(s));
 return 0;
}
报错内容如下:
eg6527a.c: In function ‘main’:
eg6527a.c:8:2: warning: ‘return’ with a value, in function returning void [enabled by default]
  return 0;
  ^
In file included from eg6527a.c:3:0:
eg6527a.c: At top level:
/usr/include/strlwr.h:1:14: warning: ‘strlwr’ used but never defined [enabled by default]
 static char *strlwr(char *s);
              ^
/tmp/cc6LXD8t.o: In function `main':
eg6527a.c:(.text+0x79): undefined reference to `strlwr'
collect2: error: ld returned 1 exit status
查找一下原因,原来是兼容性问题strlwr、strupr函数不是标准的C函数库,只能在VC中使用,

linux gcc 下需要自己定义这些函数。

具体做法如下:

自己写一个strlwr.h头文件,内容如下:

char *strlwr(char *s)
{
 char *str;
 str = s;
 while(*str != '\0')
 {
  if(*str >= 'A' && *str <= 'Z') {
     *str += 'a'-'A';
 }
 str++;
 }
 return s;
 }
将其保存在/usr/include目录下

再修改源文件为:

#include<stdio.h>
#include<string.h>
#include<strlwr.h>
void main()
{
 char s[10]={"CHINA"};
 printf("%s\n",strlwr(s));
 return 0;
}


此时再次运行程序 gcc -o 6527a 6527a.c

生成可执行文件6527a

运行该文件     ./6527a

结果为:china  

将字符串“CHINA”转换为:"china"



linux下运行strlwr函数出错:ld returned 1 exit status

标签:   c语言   linux   gcc   

原文地址:http://blog.csdn.net/wangjiaweiwei/article/details/40095751

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!