手动跑C语言“圣经”《C程序设计语言》(https://book.douban.com/subject/1139336/)自带的例子(P21页)时,编译竟然报错,一脸懵逼。
1_9.c:7:5: error: conflicting types for ‘getline’
int getline(char line[], int maxline);
^
In file included from 1_9.c:1:0:
/usr/include/stdio.h:678:20: note: previous declaration of ‘getline’ was here
extern _IO_ssize_t getline (char **__restrict __lineptr,
^
1_9.c:31:5: error: conflicting types for ‘getline’
int getline(char s[], int lim)
^
In file included from 1_9.c:1:0:
/usr/include/stdio.h:678:20: note: previous declaration of ‘getline’ was here
extern _IO_ssize_t getline (char **__restrict __lineptr,
^
看一下报错,是和“ /usr/include/ ”下的C标准库“ stdio.h ”有冲突,该文件也声明定义了“ getline ”。
所以,把原书示例中的“ getline ”函数换一下名字就OK了,编译成功。书的内容是有点老了!
root@ubuntu:~/c# gcc -o 1_9 1_9.c
root@ubuntu:~/c# ./1_9 < 1_9.c
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)