G++ 编译器找不到 iostream 所需的标头

Posted

技术标签:

【中文标题】G++ 编译器找不到 iostream 所需的标头【英文标题】:G++ compiler failed to find headers required by iostream 【发布时间】:2020-02-26 08:57:09 【问题描述】:

我正在尝试编译一个简单的 c++ 代码,其中包含 <iostream> 标头。令人惊讶的是,我面临着以前从未遇到过的错误。如果删除 <iostream> 标头,一切正常。

代码:

#include <iostream>

int main()
    return 0;

编译错误:

$ g++ temp.cpp
In file included from /usr/lib/gcc/x86_64-linux-gnu/7/include/stdint.h:9:0,
                 from /usr/include/c++/7/cstdint:41,
                 from /usr/include/c++/7/bits/char_traits.h:501,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from temp.cpp:1:
/usr/local/include/stdint.h:26:2: error: #error This header should only be used with Microsoft compilers
 #error This header should only be used with Microsoft compilers
  ^~~~~
In file included from /usr/include/c++/7/cerrno:42:0,
                 from /usr/include/c++/7/ext/string_conversions.h:44,
                 from /usr/include/c++/7/bits/basic_string.h:6361,
                 from /usr/include/c++/7/string:52,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from temp.cpp:1:
/usr/local/include/errno.h:14:10: fatal error: crtdefs.h: No such file or directory
 #include <crtdefs.h>
          ^~~~~~~~~~~
compilation terminated.

g++ 版本详情:

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.4.0-1ubuntu1~18.04.1' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)

如果我删除 &lt;iostream&gt; 标头,代码将编译。我认为在包含路径中找不到某些标头。但是这个错误对我来说很奇怪。非常感谢任何帮助。

其他日志:

$ g++ -v temp.cpp
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.4.0-1ubuntu1~18.04.1' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1) 
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/7/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase test -version -fstack-protector-strong -Wformat -Wformat-security -o /tmp/ccueFKYa.s
GNU C++14 (Ubuntu 7.4.0-1ubuntu1~18.04.1) version 7.4.0 (x86_64-linux-gnu)
    compiled by GNU C version 7.4.0, GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0, isl version isl-0.19-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/7"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/7
 /usr/include/x86_64-linux-gnu/c++/7
 /usr/include/c++/7/backward
 /usr/lib/gcc/x86_64-linux-gnu/7/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C++14 (Ubuntu 7.4.0-1ubuntu1~18.04.1) version 7.4.0 (x86_64-linux-gnu)
    compiled by GNU C version 7.4.0, GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0, isl version isl-0.19-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 38816e3807cdcb3c59571e251bd6c090
In file included from /usr/lib/gcc/x86_64-linux-gnu/7/include/stdint.h:9:0,
                 from /usr/include/c++/7/cstdint:41,
                 from /usr/include/c++/7/bits/char_traits.h:501,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from test.cpp:1:
/usr/local/include/stdint.h:26:2: error: #error This header should only be used with Microsoft compilers
 #error This header should only be used with Microsoft compilers
  ^~~~~
In file included from /usr/include/c++/7/cerrno:42:0,
                 from /usr/include/c++/7/ext/string_conversions.h:44,
                 from /usr/include/c++/7/bits/basic_string.h:6361,
                 from /usr/include/c++/7/string:52,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from test.cpp:1:
/usr/local/include/errno.h:14:10: fatal error: crtdefs.h: No such file or directory
 #include <crtdefs.h>
          ^~~~~~~~~~~
compilation terminated.

谢谢!

【问题讨论】:

/usr/local/include中的头文件是什么?显然这些头文件是错误的。我目前不知道如何配置 gcc 搜索头文件的包含路径。作为一种解决方法,您可以尝试(备份和)从 /usr/local/include 中删除文件。 你的头文件好像被纠缠了。不太可能,这是一个严重的 GCC 错误。 在这种情况下,我会尝试卸载并安装 gcc 包。但也许其他一些包在某处丢弃了一些错误的头文件。您是否在没有发行包管理器的情况下在本地安装了一些东西。 /usr/local...中有错误的东西吗? 您是否在包管理器之外手动安装了任何东西?直接进入/usr/locale 还是使用make install 之类的东西?看来您在 Ubuntu 软件包管理器之外安装了一些不属于那里的东西到 /usr/local//usr/local/include 中不应有标准库标头,除非您打算覆盖您的操作系统/发行版提供的标头,而这是不太可能的。 你在你的ubuntu上安装了cross compiler for windows吗?看起来您安装了某种交叉编译器,并为您的本机操作系统使用了这些“错误”的库...... 【参考方案1】:

你在 /usr/local/ 中有一些意想不到的东西。尝试卸载 Microsoft® 编译器、SDK、头文件等。如果您没有此类软件包,请从 /usr/local/include/ 中删除有问题的文件(或将整个目录移动到备份中)。

【讨论】:

我错误地在/usr/local/include 位置安装了一个包含文件的库,该位置的标题为stdint.h。所以 g++ 选择了这个标题而不是实际的标题,因此 crtdefs.h 被包含在内。感谢您的帮助。

以上是关于G++ 编译器找不到 iostream 所需的标头的主要内容,如果未能解决你的问题,请参考以下文章

VS2017新建或拷贝项目编译时出现:找不到 Windows SDK 版本8.1.请安装所需的版本的 Windows SDK

在目标平台上找不到应用程序所需的本机扩展“文件”的实现

为啥好多东西运行不了。出现“找不到所需的.dll文件?”

为啥 g++ 找不到 iostream.h?

找不到指令“...”所需的控制器“ngModel”

Teamcity:错误 MSB3147:找不到所需的文件“setup.bin”