GCC错误出来的代码,用来做工精细

问题描述:

我已经成功地在过去编译的程序,但现在我得到了一堆errors.The源代码就是: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笔记本电脑上的操作系统UBUNTU 12.1

+6

我*希望*你可以理解,没有一个更具体,可重现的问题演示,没有人能认真对待这个问题。 – delicateLatticeworkFever 2013-04-29 18:45:36

+2

除了goldilocks的评论,提供的错误信息表明问题出在你的代码上,这使得这是一个编程问题,尤其是因为你没有说你正在使用哪个操作系统。 – depquid 2013-04-29 18:56:42

+0

不,我在另一个系统上成功编译它... – Hanna 2013-04-29 19:01:58

从您的错误消息中,我看到您称为文件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++编译器处理,所以你看到这些错误。

+5

如果你在'xyz.cc'或'xyz.C'上运行'gcc',它会调用C++编译器... – vonbrand 2013-04-29 19:02:04

阅读creat,closeexit函数的手册页。

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

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

close()要求:

#include <unistd.h> 

exit()要求:

#include <stdlib.h> 

至于为什么代码被编译之前,很难告诉。也许编译器是以一种更宽容的模式被调用的,它不会抱怨缺少函数声明,或者你包含的某些头文件包含#include指令用于你需要的头文件。

+0

gxx_personality_v0怎么办? – Hanna 2013-04-30 15:39:41

+0

@ user1181065:对不起,我对此不太了解。无论如何,如果您使用这些功能,即使它不能直接解决您的问题,也应该包含所需的标题。当你添加这些'#include'时会发生什么? – 2013-04-30 18:20:29