首页 > 代码库 > linux下运行strlwr函数出错:ld returned 1 exit status

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

运行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