手動跑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了,編譯成功。書的内容是有點老了!
[email protected]:~/c# gcc -o 1_9 1_9.c
[email protected]:~/c# ./1_9 < 1_9.c
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)