3
votes

I have two versions of gcc/g++ installed: gcc-4.5(installed from package manager, binary files are under /usr/bin, header files are under /usr/include) and gcc-4.4.3(compiled by myself, put under /opt/gcc-4.4.3).

When I made gcc/g++ 4.4.3 to be the default version (using "update-alternatives" to make /usr/bin/gcc and /usr/bin/g++ point to the corresponding one under directory "/opt/gcc-4.4.3/bin") and compiled the files, it always reported the following errors:

/usr/include/c++/4.5/bits/basic_string.h:1659: undefined reference to `std::basic_string, std::allocator::_S_construct_aux_2(unsigned long, char, std::allocator const&)'

It seems that the compiler is trying to find the header files for c++ under /usr/include/c++/4.5, which causes the link error. When I changed the default version of gcc to gcc-4.5, the errors disappeared.

So how could I make the compiler search for the header files under the correct directory "/opt/gcc-4.4.3/include"? I have tried to export CPLUS_INCLUDE_PATH, but it seems not work.

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)
2
Did you try to add -I/full/path/to/the/header when trying to compile with 4.4.3?Kiril Kirov
@meh I'm trying to change the default search path of g++ from /usr/include to /opt/gcc-4.4.5/include, so -I/full/path/to/the/header can't solve my problem.Wei Li
Yeah, I thought so, but I wasn't sure. Can't help, sorry.Kiril Kirov
What does g++ -v say is the include path?Mats Petersson
@MatsPetersson I added it in my question.Wei Li

2 Answers

2
votes

Try compiling gcc 4.4.3 again, but use the --with-gxx-include-dir=/opt/gcc-4.4.3/include option in the configure step.

1
votes

It might be an issue with what update-alternatives has done, or not done.

When I build an alternate compiler I tend to use a --prefix and --program-suffix=-XY just so I can spot problems. Check which cpp is being run:

/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:'

(you can also check ld and as with --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 .