【问题标题】:Cannot compile simple c++ program in Ubuntu无法在 Ubuntu 中编译简单的 c++ 程序
【发布时间】:2012-02-15 23:14:51
【问题描述】:

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

#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 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

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

【问题讨论】:

  • g++ --verbose -o test test.cpp 带给你什么?
  • 我可能已经解决了这个问题。我检查了详细的输出并决定简化路径。我将它更改为只是 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 程序现在编译,但运行时不输出任何内容。这正常吗?
  • 您是否安装了build-essential-package? (sudo apt-get install build-essential)
  • 我早期学到的一件事是,永远不要在 UNIX(或类 UNIX)机器上将程序命名为“测试”!和你一样,我很难学到这一点。 :) 有关test 命令的描述,请执行man test

标签: c++ compiler-errors ubuntu-11.10


【解决方案1】:

您的代码适用于我在同一平台上。

错误消息看起来像 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++ 库,而无需了解具体细节。

【讨论】:

  • 谢谢,不过我确实想通了
【解决方案2】:

我遇到了与此非常相似的问题。就我而言,问题是我有一些损坏的头文件,这可以通过尝试查看它们来证明:

/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

【讨论】:

  • 您也可以使用aptitude reinstall重新安装软件包。
  • 如果有人没有安装aptitudesudo apt-get install --reinstall libc6-dev 也可以。
  • apt-get purge 删除了我之前安装的所有内容。经历了不得不重新安装它们的麻烦。
  • 在尝试了我能想到的所有方法之后,这才成功。谢谢!
【解决方案3】:

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

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

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

export PATH: $PATH:./
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
  • 2016-02-23
  • 1970-01-01
相关资源
最近更新 更多