3

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

当我将 gcc/g++ 4.4.3 设置为默认版本时(使用“update-alternatives”使 /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::allocator::_S_construct_aux_2(unsigned long, char, std::allocator const&)'

似乎编译器试图在 /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)
4

2 回答 2

2

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

于 2013-02-20T13:07:26.747 回答
1

这可能是update-alternatives已完成或未完成的问题。

当我构建一个备用编译器时,我倾向于使用 a--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:'

(您也可以检查ld和)as--print-prog-name

Setting CPPFLAGS="-v -H" during a build may help track things down too.

An ugly workaround might be CPPFLAGS="-nostdinc -nostdinc++ -I/opt/gcc-4.4.3/include/" but it's better to fix your compile environment, as that's likely to cause as many problems as it solves. There are also options -isystem and -sysroot to help in certain cases, see http://gcc.gnu.org/onlinedocs/cpp/Invocation.html .

于 2013-02-20T13:41:00.247 回答