在Ubuntu中无法编译简单的C++程序

5

我尝试在终端中构建一个简单程序。

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

我执行了 g++ -o test 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 beforeextern’
/usr/include/stdlib.h:145: error: expected constructor, destructor, or type conversion beforeextern’
/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

列表如此继续。我希望有人能指出我还没有做什么来使这个工作。


g++ --verbose -o test test.cpp 会给你什么结果? - genpfault
1
请查看http://ubuntuforums.org/showthread.php?t=1877944,看看是否有帮助。 - user312650
我可能已经解决了这个问题。我检查了详细输出并决定简化路径。我将其更改为/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin程序现在可以编译,但运行时没有任何输出。这正常吗? - Nick Schudlo
1
你安装了 build-essential 包吗?(sudo apt-get install build-essential) - Magnus Hoff
当运行程序时,我没有意识到需要在它之前加上"./"。我只是输了"test",但实际上需要输入"./test"。 - Nick Schudlo
1
我早期学到的一件事是,在UNIX(或类UNIX)机器上永远不要将程序命名为“test”!就像你一样,我也是通过吃亏才明白这个道理的。 :) 要查看“test”命令的描述,请执行“man test”。 - Some programmer dude
3个回答

4

在我使用相同平台时,您的代码可以正常工作。

错误信息看起来像是C语言的错误。也许使用C++头文件会有所帮助。

#include <cstdio>
#include <cstdlib>

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

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

tom@flim:~$ set | grep g++

tom@flim:~$ alias grep
alias grep='grep --color=auto'

tom@flim:~$ alias g++
bash: alias: g++: not found

tom@flim:~$ which g++
/usr/bin/g++

tom@flim:~$ ll `which g++`
lrwxrwxrwx 1 root root 7 2011-08-14 02:17 /usr/bin/g++ -> g++-4.6*

tom@flim:~$ 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++库,而无需知道琐碎的细节。

谢谢,不过我已经想通了。 - Nick Schudlo

4

我有一个与此非常相似的问题。在我的情况下,问题是因为我有一些损坏的头文件,可以通过查看它们来证明:

/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
1
如果某人没有安装aptitudesudo apt-get install --reinstall libc6-dev也可以。 - ElmoVanKielmo
apt-get purge 删除了我之前安装的所有内容。现在又要费劲地重新安装它们。 - user3124361
在尝试了我能想到的一切之后,这个方法终于让它工作了。谢谢! - user113478

2

解决方案:我的路径是空的,因为之前尝试过让它工作。我使用以下方法创建了一个干净的路径:

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

编译后我的问题是程序没有显示任何结果。这是因为作为新的 Linux 用户,我没有意识到需要在程序前面加上“./”来调用程序。这也可以通过以下方式在路径中设置:

export PATH: $PATH:./

1
在你的 PATH 中有 . 可能会非常危险。 - Léo Lam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接