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

时间:2013-04-29 18:42:16

标签: 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);
}

现在我收到了错误:

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

有时我会收到有关gxx_personality_v0的错误,有时我根本没有错误!我已尝试更新gcc,但问题仍然存在。出了什么问题? vaio笔记本电脑上的OS UBUNTU 12.1

2 个答案:

答案 0 :(得分: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

$ 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]

如评论中所述,扩展名为.C的文件由C ++编译器处理,因此您会看到这些错误。

答案 1 :(得分:-1)

阅读creatcloseexit函数的手册页。

在我的系统上,creat()需要:

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

close()要求:

#include <unistd.h>

exit()要求:

#include <stdlib.h>

至于代码之前编译的原因,很难说。也许编译器是在更宽松的模式下调用的,并没有抱怨缺少函数声明,或者你所做的一些标题包含了你需要的标题的#include指令。