简体   繁体   English

g ++失败,并向标准C ++库显示“未定义的引用”错误

[英]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. 我已经构建并安装了g ++,并在一个简单的“ Hello World”程序上对其进行了测试,它似乎可以正常工作。

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. 因此很明显,C ++标准库的某些部分已正确安装。 This is not simply an "obvious" installation bug that does not have libstdc++ installed. 这不仅仅是一个没有安装libstdc ++的“明显”安装错误。

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 : 库路径为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. 感谢用户的评论,我看了一下link命令,显然有一个问题。 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 / 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. 注意“ -L”链接到旧版本的gcc。 The question then is how to tell cmake where the libraries are. 然后的问题是如何告诉cmake库在哪里。 I have no idea how to do that. 我不知道该怎么做。 That was the point of using LD_LIBRARY_PATH. 那就是使用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++. 与此相关的是,令我有些困惑的是,cmake找不到$ path中的g ++编译器,而是恢复为/ 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++. 然后,我不得不编辑CMakeCache.txt文件,以使其使用实际上在路径上的g ++而不是/ 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. 1)定义环境变量CC(= gcc)和CCX(= g ++)指向我们正在使用的gcc编译器。 cmake uses these environment variables to finc the c and c++ compilers. cmake使用这些环境变量来查找c和c ++编译器。

2) Set up LD_LIBRARY_PATH to the path of the corresponding library support for the gcc and g++ compilers. 2)将LD_LIBRARY_PATH设置为gcc和g ++编译器的相应库支持的路径。

3) Make sure the correct compilers are on the path. 3)确保正确的编译器在路径上。

Until we did this, cmake would not configure properly and the compile would fail. 在执行此操作之前,cmake将无法正确配置,并且编译将失败。

These steps appear to work for a compiler built in a non-standard location. 这些步骤似乎适用于在非标准位置构建的编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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