2012-02-15 124 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