2013-03-08 2 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; 
} 

내가 우분투를 사용하고 있습니다 (그리고 솔라리스 및 BSD에도 작동하도록 코드를 싶습니다).

readwrite에 대한 테스트

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

답변

11

, 당신은

#include <unistd.h> 

에 그들이 stdio.h에 선언하지 않을 수 있습니다.

+0

우분투에서 작동합니다. 나는 또한 solaris와 bsd에서도 작동하길 바랍니다. 감사. –

+1

어쨌든'man read'는 포함 할 헤더를 알려줍니다. –

관련 문제