2012-02-15 112 views
5

我试图在终端中构建一个简单的程序。无法在Ubuntu中编译简单的C++程序

#include <stdio.h> 
#include <stdlib.h> 
int main() 
{ 
     printf("TESTING"); 
     return 1; 
} 

我跑的g ++ -o测试TEST.CPP

的错误:

/usr/include/features.h:323:26: error: bits/predefs.h: No such file or directory 
/usr/include/features.h:356:25: error: sys/cdefs.h: No such file or directory 
/usr/include/features.h:388:23: error: gnu/stubs.h: No such file or directory 
In file included from test.cpp:2: 
/usr/include/stdlib.h:42:29: error: bits/waitflags.h: No such file or directory 
/usr/include/stdlib.h:43:30: error: bits/waitstatus.h: No such file or directory 
/usr/include/stdlib.h:320:49: error: sys/types.h: No such file or directory 
In file included from test.cpp:2: 
/usr/include/stdlib.h:35: error: ‘__BEGIN_DECLS’ does not name a type 
/usr/include/stdlib.h:102: error: expected constructor, destructor, or type conversion before ‘;’ token 
/usr/include/stdlib.h:113: error: ‘__END_NAMESPACE_STD’ does not name a type 
/usr/include/stdlib.h:122: error: expected constructor, destructor, or type conversion before ‘;’ token 
/usr/include/stdlib.h:140: error: expected constructor, destructor, or type conversion before ‘extern’ 
/usr/include/stdlib.h:145: error: expected constructor, destructor, or type conversion before ‘extern’ 
/usr/include/stdlib.h:149: error: expected initializer before ‘__THROW’ 
/usr/include/stdlib.h:152: error: expected initializer before ‘__THROW’ 
/usr/include/stdlib.h:153: error: ‘__END_NAMESPACE_STD’ does not name a type 
/usr/include/stdlib.h:160: error: ‘__END_NAMESPACE_C99’ does not name a type 
/usr/include/stdlib.h:168: error: ‘__END_NAMESPACE_STD’ does not name a type 

名单还在继续这种方式。我希望有人能指出我没有做的工作。

+0

什么'g ++ --verbose -o test test.cpp给你? – genpfault 2012-02-15 23:24:48

+1

看看http://ubuntuforums.org/showthread.php?t=1877944有帮助 – 2012-02-15 23:30:35

+0

我可能已经解决了这个问题。我检查了详细的输出并决定简化路径。我将它改为 /usr/local/sbin:/ usr/local/bin:/ usr/sbin:/ usr/bin:/ sbin:/ bin 程序现在编译,但运行时不输出任何内容。这是正常的吗? – 2012-02-15 23:44:07

回答

2

解决方法:由于之前尝试使其工作,我的路径为空。我创建了一个干净的路径使用:

export PATH= /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 

我编译后的问题是,该程序不会显示任何结果。这是因为作为一个新的Linux用户,我没有意识到我需要用./在前面调用一个程序。这可以设置路径以及通过呼吁:

export PATH: $PATH:./ 
+1

[在PATH中有'.'可能会非常危险。](https://unix.stackexchange。com/questions/65700/is-it-safe-add-to-my-path-how-come) – 2015-07-31 05:45:57

4

您的代码适用于我的平台。

错误消息看起来像C错误。也许使用C++头文件会有所帮助。

#include <cstdio> 
#include <cstdlib> 

int main(int argc, char *argv[]) { 
    printf("TESTING"); 
    return 0; 
} 

你也可能有一些奇怪的别名。有时候人们会错误地将gcc设置为g ++的别名。

[email protected]:~$ set | grep g++ 

[email protected]:~$ alias grep 
alias grep='grep --color=auto' 

[email protected]:~$ alias g++ 
bash: alias: g++: not found 

[email protected]:~$ which g++ 
/usr/bin/g++ 

[email protected]:~$ ll `which g++` 
lrwxrwxrwx 1 root root 7 2011-08-14 02:17 /usr/bin/g++ -> g++-4.6* 

[email protected]:~$ g++ --version 
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 
Copyright (C) 2011 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

这是怎么设置我的开发环境在Ubuntu:

sudo apt-get install build-essential 

这将设置所有的标准C++库,而无需知道knitty坚韧不拔的细节。

+0

谢谢,我弄明白了,虽然 – 2012-02-16 00:00:13

3

我有一个非常类似的问题,这一点。在我的情况的问题是,我有证明,试图查看他们一些损坏的头文件:

/usr/include/x86_64-linux-gnu/sys$ cat * | grep "Input/outpu error" 
cat: ioctl.h: Input/output error 
cat: types.h: Input/output error 

对我来说,解决办法就是清除这些文件,然后重新安装它们。

sudo apt-get purge libc6-dev 
sudo apt-get install libc6-dev 
+1

您也可以使用'aptitude reinstall'重新安装软件包。 – tmandry 2013-10-06 22:39:08

+1

如果有人没有安装'aptitude':'sudo apt-get install --reinstall libc6-dev'也可以。 – ElmoVanKielmo 2014-06-10 06:07:17

+0

apt-get purge删除了之前安装的所有内容。经历再次安装它们的麻烦。 – user3124361 2015-01-13 12:28:45