2013-03-08 59 views
0

我希望我的程序能夠在沒有警告的情況下與clang一起編譯。該功能似乎在編譯時工作,但爲什麼?我該如何處理警告?如何處理來自clang編譯器的警告?

$ clang cpu-disk-info.c 
cpu-disk-info.c:108:17: warning: implicit declaration of function 'read' is 
     invalid in C99 [-Wimplicit-function-declaration] 
    while ((n = read(0, buf, betterSIZE)) > 0) 
       ^
cpu-disk-info.c:109:5: warning: implicit declaration of function 'write' is 
     invalid in C99 [-Wimplicit-function-declaration] 
    write(1, buf, n); 
    ^

代碼

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

#define SIZE 1024 
#define betterSIZE 4*SIZE /* read a better size at a time */ 

int main(int argc, char **argv) 
{ 

    /* copy(); */ 

    /* make the names known */ 

    void info(char file_name[]); 
    void buffered(char file_name[]); 
    void better_buffered(char file_name[]); 

    /* test */ 

    clock_t toc; 
    clock_t tic = clock(); 
    info("coreutils_8.13.orig.tar.gz"); 
    info("coreutils_8.13.orig.tar.gz"); 
    info("coreutils_8.13.orig.tar.gz"); 
    info("coreutils_8.13.orig.tar.gz"); 
    info("coreutils_8.13.orig.tar.gz"); 
    toc = clock(); 
    printf("Unbuffered: %f seconds\n", (double)(toc - tic)/CLOCKS_PER_SEC); 
    tic = clock();  
    buffered("coreutils_8.13.orig.tar.gz"); 
    buffered("coreutils_8.13.orig.tar.gz"); 
    buffered("coreutils_8.13.orig.tar.gz"); 
    buffered("coreutils_8.13.orig.tar.gz"); 
    buffered("coreutils_8.13.orig.tar.gz"); 
    toc = clock(); 
    printf("Buffered: %f seconds\n", (double)(toc - tic)/CLOCKS_PER_SEC); 
    tic = clock();  
    better_buffered("coreutils_8.13.orig.tar.gz"); 
    better_buffered("coreutils_8.13.orig.tar.gz"); 
    better_buffered("coreutils_8.13.orig.tar.gz"); 
    better_buffered("coreutils_8.13.orig.tar.gz"); 
    better_buffered("coreutils_8.13.orig.tar.gz"); 
    toc = clock(); 
    printf("Better buffered: %f seconds\n", (double)(toc - tic)/CLOCKS_PER_SEC); 
    return 0; 
} 

void info(char file_name[]) 
{ 
    int ch; 
    FILE *fp; 
    fp = fopen(file_name,"r"); 
    // read mode 
    if (fp == NULL) 
    { 
     perror(file_name); 
     exit(EXIT_FAILURE); 
    } 
    while ((ch = fgetc(fp)) != EOF) 
    { 
     //putchar(ch); 
    } 
    fclose(fp); 
} 

void buffered(char file_name[]) 
{ 
    char buf[SIZE]; 
    FILE *fp; 
    size_t nread; 
    fp = fopen(file_name, "r"); 
    if (fp) { 
     while ((nread = fread(buf, 1, sizeof buf, fp)) > 0) 
    { 
      //fwrite(buf, 1, nread, stdout); 
    } 
     if (ferror(fp)) { 
      /* to do: deal with error */ 
     } 
     fclose(fp); 
    } 
} 


void better_buffered(char file_name[]) 
{ 
    char buf[betterSIZE]; 
    FILE *fp; 
    size_t nread; 
    fp = fopen(file_name, "r"); 
    if (fp) { 
     while ((nread = fread(buf, 1, sizeof buf, fp)) > 0) 
    { 
      //fwrite(buf, 1, nread, stdout); 
    } 
     if (ferror(fp)) { 
      /* to do: deal with error */ 
     } 
     fclose(fp); 
    } 
} 

int copy() /* input2output ie anything to anything */ 
{ 
    char buf[betterSIZE]; 
    int n; 
    while ((n = read(0, buf, betterSIZE)) > 0) 
    write(1, buf, n); 
    return 0; 
} 

我使用Ubuntu(和希望的代碼也工作在Solaris和BSD)。

測試

$ ./benchm 
Unbuffered: 0.670000 seconds 
Buffered: 0.040000 seconds 
Better buffered: 0.020000 seconds 

回答

11

對於readwrite,你必須

#include <unistd.h> 

他們不是在stdio.h聲明。

+0

它適用於Ubuntu。我希望它也適用於solaris和bsd。謝謝。 – 2013-03-08 14:26:47

+1

在那裏應該是一樣的,無論如何,'man read'告訴你要包含哪些頭部。 – 2013-03-08 14:29:23