c - 如何处理来自 clang 编译器的警告?

标签 c clang c99

我希望我的程序能够在没有警告的情况下使用 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

最佳答案

对于 readwrite , 你必须

#include <unistd.h>

它们没有在 stdio.h 中声明.

关于c - 如何处理来自 clang 编译器的警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15296067/

相关文章:

c++ - 如何将标准库与 C++ 模块一起使用? (例如 : `import std.io` )

c++ - 不是最后一个模板参数的推导和参数包 : is this valid code?

C++17 编译器不应该发现对未定义值的引用传递吗?

c - 使用size_t获取数组的大小。另外,我如何使用malloc动态分配内存到数组?

c - 释放 C 函数使用的内存时出错

c - 如何在C99中定义一个函数为inline internal and external copy

c - C89、C90 或 C99 中的所有函数都需要原型(prototype)吗?

c - 我怎样才能弄清楚我的 C 编译器 GCC 使用的默认标准是什么?

c - #if defined MACRO 是否等同于#ifdef MACRO?

c - OpenMPI 中的 Scanf 和 Printf