Gcc在错误的目录下搜索头文件

Gcc searches for header files under the wrong directory

本文关键字:搜索 文件 错误 Gcc      更新时间:2023-10-16

我安装了两个版本的gcc/g++:gcc-4.5(从软件包管理器安装,二进制文件在/usr/bin下,头文件在/usr/include下)和gcc-4.4.3(我自己编译,放在/opt/gcc-4.4.3下)。

当我将gcc/g++4.4.3设置为默认版本(使用"更新选项"使/usr/bin/gcc和/usr/bin/g++指向目录"/opt/gcc-4.4.3/bin"下的相应版本)并编译文件时,它总是报告以下错误:

/usr/include/c++/4.5/bits/basic_string.h:1659:对的未定义引用`std::basic_string,std::分配器::_S_construct_aux_2(unsigned long,char,std::分配器常量&)'

编译器似乎试图在/usr/include/c++/4.5下查找c++的头文件,这导致了链接错误。当我将gcc的默认版本更改为gcc-4.5时,错误消失了。

那么,我如何让编译器在正确的目录"/opt/gcc-4.4.3/include"下搜索头文件呢?我曾尝试导出CPLUS_INCLUDE_PATH,但似乎不起作用。

PS:gcc-v

Using build-in specs
Target: x86_64-suse-linux
Configured with: ./configure --prefix=/opt/gcc-4.4.3
Thread model: posix
gcc version 4.4.3 (GCC)

再次尝试编译gcc 4.4.3,但在配置步骤中使用--with-gxx-include-dir=/opt/gcc-4.4.3/include选项。

这可能是update-alternatives做了什么或没有做什么的问题。

当我构建备用编译器时,我倾向于使用--prefix--program-suffix=-XY,这样我就可以发现问题。检查哪个cpp正在运行:

/opt/gcc-4.4.3/bin/g++ --print-prog-name=cpp
cpp -v </dev/null
/opt/gcc-4.4.3/bin/cpp -v < /dev/null
/opt/gcc-4.4.3/bin/g++ -print-search-dirs | grep '^programs:'

(您也可以用--print-prog-name检查ldas

在构建过程中设置CPPFLAGS="-v -H也可能有助于追踪情况

一个丑陋的解决方法可能是CPPFLAGS="-nostdinc -nostdinc++ -I/opt/gcc-4.4.3/include/",但最好修复编译环境,因为这可能会导致与解决的问题一样多的问题。在某些情况下,还有选项-isystem-sysroot可提供帮助,请参阅http://gcc.gnu.org/onlinedocs/cpp/Invocation.html。