简体   繁体   中英

g++ fails with “undefined reference” errors to the standard C++ library

I have built and installed g++ and tested it on a simple "Hello World" program and it appears to work.

However, for our larger code, the compile fails with errors such as:

CMakeFiles/gaim_convert.dir/GaimConvert.cpp.o: In function `Output(std::string const&, std::ostream&)':GaimConvert.cpp:(.text._Z6OutputRKSsRSo[_Z6OutputRKSsRSo]+0x12): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& st d::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)'

The program that works correctly is:

#include <iostream>

int main() {
  std::cout << "Hello World!" << std::endl;
  std::cin.get();
  return 0;
}

So clearly certain parts of the C++ standard library are installed correctly. This is not simply an "obvious" installation bug that does not have libstdc++ installed.

The code will compile with a different version of the compiler, so it's not the code.

What is a way to debug the installation so that this error message is removed? The library path is LD_LIBRARY_PATH :

/tec/mannucci/gccBuild/lib64:/tec/mannucci/gccBuild/lib:/usr/local/gmp510/lib:/usr/local/mpfr311/lib:/usr/local/mpc101/lib:/usr/local/ppl011/lib:/usr/local/cloog0162/lib:/usr/local/lib64:/usr/lib64:...

Thanks to a user comment, I looked at the link command, and there's clearly a problem. This is likely a cmake issue. Here is the link command:

/tec/mannucci/gccBuild/bin/g++ -fmessage-length=0 -O3 -DNDEBUG CMakeFiles/gaim_convert.dir/GaimConvert.cpp.o -o gaim_convert -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 -rdynamic ../../lib/geometry/libgeometry.a ../../lib/util/libutil.a ../../ctimetrans/libtimetrans.a ../../libversion.a

Note the '-L' links to an older version of gcc. The question then is how to tell cmake where the libraries are. I have no idea how to do that. That was the point of using LD_LIBRARY_PATH. I thought that would address the issue.

On a related note, it is somewhat disconcerting to me that cmake did not find the g++ compiler that is in the $path, but reverted to /usr/bin/c++. I then had to edit the CMakeCache.txt file to get it to use the g++ that is in fact on the path instead of /usr/bin/c++.

We were able to address this issue as follows:

1) Define environment variables CC (=gcc) and CCX (=g++) to point to the gcc compilers we are using. cmake uses these environment variables to finc the c and c++ compilers.

2) Set up LD_LIBRARY_PATH to the path of the corresponding library support for the gcc and g++ compilers.

3) Make sure the correct compilers are on the path.

Until we did this, cmake would not configure properly and the compile would fail.

These steps appear to work for a compiler built in a non-standard location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM