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"