K&R Exercise 1-16 clang - conflicting types for ge

2019-07-04 05:26发布

问题:

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.

回答1:

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.



回答2:

Here is a similar question: Why do I get a "conflicting types for getline" error when compiling the longest line example in chapter 1 of K&R2?

It's the same issue but with gcc. A solution is to put the compiler into ANSI C mode, which disables GNU/POSIX extensions.

Try the following:

$ clang test.c -ansi 

or alternatively

$ clang test.c -std=c89

Tested sucessfully on my machine:

$ clang --version
clang version 3.3 (tags/RELEASE_33/rc2)
Target: x86_64-redhat-linux-gnu
Thread model: posix

With this compiler on a machine at my university it was not even required to specify the ANSI mode for successful compilation:

->clang --version
Apple clang version 1.7 (tags/Apple/clang-77) (based on LLVM 2.9svn)
Target: x86_64-apple-darwin10
Thread model: posix


标签: c clang