GCC在以前工作正常的代码上出错

Han*_*nna 0 filenames gcc

我有一个程序已经成功编译过去,但现在我得到了一堆错误.源代码只是:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

int main()
{
    int fd;
    fd = creat("datafile.dat", S_IREAD | S_IWRITE);
    if (fd == -1)
        printf("Error in opening datafile.dat\n");
    else
    {
        printf("datafile.dat opened for read/write access\n");
        printf("datafile.dat is currently empty\n");
    }
    close(fd);
    exit (0);
}
Run Code Online (Sandbox Code Playgroud)

现在我得到了错误:

cre.C:8:54: error: ‘creat’ was not declared in this scope
cre.C:16:17: error: ‘close’ was not declared in this scope
cre.C:17:16: error: ‘exit’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

有时候我会收到一个错误gxx_personality_v0,有时我根本就没有错误!我尝试过更新gcc,但问题仍然存在.出了什么问题?在Vaio笔记本电脑上的OS UBUNTU 12.1

Ren*_*nan 5

从您的错误消息中我看到您调用了您的文件cre.C.gcc对文件名区分大小写:尝试命名cre.c并编译它.

$ LANG=C cc -o foo foo.C
foo.C: In function 'int main()':
foo.C:8:54: error: 'creat' was not declared in this scope
foo.C:16:17: error: 'close' was not declared in this scope
foo.C:17:16: error: 'exit' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

$ LANG=C cc -o foo foo.c
foo.c: In function 'main':
foo.c:17:9: warning: incompatible implicit declaration of built-in function 'exit' [enabled by default]
Run Code Online (Sandbox Code Playgroud)

如注释中所述,具有.C扩展名的文件由C++编译器处理,因此您可以看到这些错误.