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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9303037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:37:58  来源:igfitidea点击:

Cannot compile simple c++ program in Ubuntu

c++compiler-errorsubuntu-11.10

提问by Nick Schudlo

I tried to build a simple program in the terminal.

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

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

I ran g++ -o test test.cpp

我跑了 g++ -o test test.cpp

The errors:

错误:

/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

The list continues this way. Im hoping someone can point out what I haven't done to make this work.

该列表以这种方式继续。我希望有人能指出我还没有做些什么来完成这项工作。

采纳答案by Nick Schudlo

SOLUTION: My path was empty due to some previous attempts at making it work. I created a clean path using:

解决方案:由于之前的一些尝试,我的路径是空的。我使用以下方法创建了一条干净的路径:

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

My problem after compiling was that the program wouldn't show any results. This was due to the fact that as a new linux user I didnt realize I needed to call a program with ./ in front. This can be set in the path as well by calling:

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

export PATH: $PATH:./

回答by Tom Kerr

Your code works for me with the same platform.

您的代码在同一平台上对我有用。

The error messages look like C errors. Perhaps using the C++ headers will help.

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

#include <cstdio>
#include <cstdlib>

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

You may also have some weird aliases. Sometimes people setup gcc as an alias for g++ incorrectly.

您可能还有一些奇怪的别名。有时人们错误地将 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.

This is how I setup my dev environment in ubuntu:

这是我在 ubuntu 中设置开发环境的方式:

sudo apt-get install build-essential

This sets up all the standard C++ libraries without needing to know the knitty gritty details.

这将设置所有标准 C++ 库,而无需了解复杂的细节。

回答by dkinzer

I was having a very similar issue to this. In my case the issue was that I had some corrupted header files as evidenced by trying to view them:

我遇到了与此非常相似的问题。在我的情况下,问题是我有一些损坏的头文件,通过尝试查看它们来证明:

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

The solution for me was to purge these files and then re-install them.

我的解决方案是清除这些文件,然后重新安装它们。

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