簡體   English   中英

K&R練習1-16 clang - getline的沖突類型

[英]K&R Exercise 1-16 clang - conflicting types for getline

我正在使用K&R,使用Clang作為我的編譯器。

使用Clang編譯時,練習1-16會產生“getline'的沖突類型”錯誤。 我猜是因為其中一個默認庫有一個getline函數。

在編譯K&R練習時,我應該向Clang傳遞哪些選項以避免包含任何其他內容?

要修改的運動樣本是:

#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;
}

調用時來自Clang的錯誤: 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.

問題只是你的系統已經提供了一個名為getline的函數。 man getline應該告訴你它的簽名。 在我的系統上它是:

ssize_t getline(char ** restrict linep, size_t * restrict linecapp, FILE * restrict stream);

你可以匹配它,或者只是將你的函數重命名為'mygetline'或類似的東西。

或者,如果您可以避免包含stdio.h ,則可以完全避免此問題。

至於你的最后一個問題:

在編譯K&R練習時,我應該向Clang傳遞哪些選項以避免包含任何其他內容?

你不能 - 系統標題就是它們,並且自從K&R於1988年最后一次修訂以來可能已經移動了。從那時起已經有多個C標准更新。 從某些方面來說,K&R真正開始長期存在。

這是一個類似的問題: 為什么在編譯K&R2第1章中的最長行示例時,我會得到“getline的沖突類型”錯誤?

這是同樣的問題,但與gcc。 解決方案是將編譯器置於ANSI C模式,該模式禁用GNU / POSIX擴展。

請嘗試以下方法:

$ clang test.c -ansi 

或者

$ clang test.c -std=c89

在我的機器上成功測試:

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

使用這個編譯器在我大學的機器上,甚至不需要為成功編譯指定ANSI模式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM