K&安培; R运动1-16铛 - 冲突的类型函数getline [英] K&R Exercise 1-16 clang - conflicting types for getline
问题描述
我通过K&放工作; R,使用锵为我的编译器
在铿锵编译练习1-16产生冲突的类型'函数getline'的错误。我猜,因为默认库中的一个具有getline函数的。
我应该通过什么选项锵编译K&放时;被列入ř练习以免别的
要修改的练习样本:
的#include<&stdio.h中GT;
#定义MAXLINE 1000INT函数getline(焦线[],INT MAXLINE);
无效副本(字符为[],在[]字符);/ *打印最长一行* /
主要()
{
INT LEN; / *当前行的长度* /
INT最大; / *最大线lenght迄今为止看到的* /
焦线[MAXLINE] / *当前输入行* /
烧焦最长[MAXLINE] / *最长行保存在这里* / 最大= 0; 而((LEN =函数getline(线,MAXLINE))大于0)
如果(LEN>最大){
最大= LEN;
副本(最长的,行); / *线 - >最长的* /
} 如果(最大值大于0)/ *有一个线路* /
的printf(\\ n \\ nLength数:%d \\ nString:%S,最大-1,最长的);
返回0;
}/ *函数getline:读取一行到s,返回长度* /
INT函数getline(char中[],INT LIM)
{
INT C,I; 对于(i = 0; I< LIM-1安培;及(C =的getchar())= EOF和放大器;!&安培;!C ='\\ n'; ++ I)
S [i] = C; 如果(C =='\\ n'){
S [i] = C;
++我;
} S [我] ='\\ 0';
返回我;
}/ *副本:副本,从进到;承担足够大* /
无效副本(字符为[],在[]字符)
{
INT I; I = 0; 而((在[I] =从[I])!='\\ 0')
++我;
}
这锵的错误,当调用为: CC ex1-16.c -o ex1-16
ex1-16.c:4:5:错误:冲突的类型'函数getline
INT函数getline(焦线[],INT MAXLINE);
^
/usr/include/stdio.h:449:9:注意:previous声明是在这里
ssize_t供函数getline(字符** __restrict,为size_t * __restrict,FILE * ...
^
ex1-16.c:17:38:错误:太少参数函数调用,预计3,有2个
而((LEN =函数getline(线,MAXLINE))大于0)
~~~~~~~ ^
/usr/include/stdio.h:449:1:注意:函数getline在这里声明
ssize_t供函数getline(字符** __restrict,为size_t * __restrict,FILE * ...
^
ex1-16.c:29:5:错误:冲突的类型'函数getline
INT函数getline(char中[],INT LIM)
^
/usr/include/stdio.h:449:9:注意:previous声明是在这里
ssize_t供函数getline(字符** __restrict,为size_t * __restrict,FILE * ...
^
产生3个错误。
问题就是,你的系统中已经提供了一个名为功能函数getline
。 男人函数getline
应该告诉你它的签名。在我的系统是:
ssize_t供函数getline(字符**限制linep,为size_t *限制linecapp,FILE *流限制);
您可以匹配或只是重命名功能,被称为mygetline'或类似的东西。
另外,如果你能避免包括 stdio.h中
,你能避免这个问题完全。
至于你的最后一个问题:
我应该通过什么选项锵编译K&放时;被列入ř练习以免别的
块引用>您不能 - 系统头是它们是什么,并有因为K&放presumably移动了; R是最后一次在1988年修订后的自那时以来,已经有多个C标准的更新。在某些方面,K&安培; R为真正开始获得在牙齿长
I'm working through K&R, using Clang as my compiler.
Exercise 1-16 produces the "conflicting types for 'getline'" error when compiled with Clang. I'm guessing because one of the default libraries has a getline function.
What options should I pass to Clang when compiling K&R exercises so as to avoid anything else being included?
The exercise sample to be modified is:
#include <stdio.h> #define MAXLINE 1000 int getline(char line[], int maxline); void copy(char to[], char from[]); /* print longest input line */ main() { int len; /* current line length */ int max; /* maximum line lenght seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if ( len > max) { max = len; copy(longest, line); /* line -> longest */ } if (max > 0) /* there was a line */ printf("\n\nLength: %d\nString: %s", max -1, longest); return 0; } /* getline: read a line into s, return length */ int getline(char s[], int lim) { int c,i; for (i=0; i<lim-1 && (c=getchar()) != EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } /* copy: copy "from" into "to"; assume to is big enough */ void copy(char to[], char from[]) { int i; i = 0; while((to[i] = from[i]) != '\0') ++i; }
The errors from Clang when invoked as:
cc ex1-16.c -o ex1-16
ex1-16.c:4:5: error: conflicting types for 'getline' int getline(char line[], int maxline); ^ /usr/include/stdio.h:449:9: note: previous declaration is here ssize_t getline(char ** __restrict, size_t * __restrict, FILE *... ^ ex1-16.c:17:38: error: too few arguments to function call, expected 3, have 2 while ((len = getline(line, MAXLINE)) > 0) ~~~~~~~ ^ /usr/include/stdio.h:449:1: note: 'getline' declared here ssize_t getline(char ** __restrict, size_t * __restrict, FILE *... ^ ex1-16.c:29:5: error: conflicting types for 'getline' int getline(char s[], int lim) ^ /usr/include/stdio.h:449:9: note: previous declaration is here ssize_t getline(char ** __restrict, size_t * __restrict, FILE *... ^ 3 errors generated.
解决方案The problem is just that your system already provides a function called
getline
.man getline
should tell you its signature. On my system it's:ssize_t getline(char ** restrict linep, size_t * restrict linecapp, FILE * restrict stream);
You can either match that or just rename your function to be called 'mygetline' or something like that.
Alternately, if you can avoid including
stdio.h
, you can avoid the problem entirely.As to your final question:
What options should I pass to Clang when compiling K&R exercises so as to avoid anything else being included?
You can't - the system headers are what they are, and have presumably moved on since K&R was last revised in 1988. There have been multiple C standard updates since then. In some ways K&R is really starting to get long in the tooth.
这篇关于K&安培; R运动1-16铛 - 冲突的类型函数getline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!