链接错误“未定义的引用";关于编译过程中的boost静态库 [英] Linking error "undefined reference" about boost static library during compiling

查看:110
本文介绍了链接错误“未定义的引用";关于编译过程中的boost静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 boost 库 1.57 在 Linux x64 上编译我的 C++ 项目.

I am trying to compile my C++ project on Linux x64 with boost libraries 1.57.

这个项目是用 scons 编译的,我在 Arch Linux 上编译成功,但这次在 Ubuntu 机器上编译失败.

This project is compiled with scons and I successfully compiled it on my Arch Linux, but this time failed on a Ubuntu machine.

我为链接添加了 -lboost_coroutine 标志,但错误未定义引用"仍然存在.

I added -lboost_coroutine flag for linking but the error "undefined reference" still existed.

/usr/bin/g++ -o build/gcc.release/app -pthread -g
build/gcc.release/src/han/unity/rpcx.o 
-lpthread -lz -lboost_coroutine -lboost_context -lboost_date_time 
build/gcc.release/src/han/unity/rpcx.o: In function `attributes':
/usr/local/include/boost/coroutine/attributes.hpp:31: undefined reference 
to `boost::coroutines::stack_traits::default_size()'

我注意到 attributes.hpp 正是 boost 协程头文件之一.我尝试使用 nm 来提升协程库,似乎没有问题.

I noticed that the attributes.hpp is exactly one of the boost coroutine header files. And I tried with nm to boost coroutine libs and it seems no problem.

nm /usr/local/lib/libboost_coroutine.a | grep "default_size"
0000000000000170 T _ZN5boost10coroutines12stack_traits12default_sizeEv

我搜索了这个错误的可能原因,大多数是关于链接器标志的顺序.而在这种情况下,rpcx.o 依赖于 boost_coroutine,所以这个出现在前面.

I searched the possible reasons for this error and most are about the order of linker flags. And in this case the rpcx.o depends on boost_coroutine, so this appears in front.

还有什么原因?

推荐答案

经过几个小时的努力,终于解决了.

After several hours effort, I finally solved it.

原来是旧的 bo​​ost (v1.55) 库仍然存在,它具有与我手动安装的 boost 库 (v1.57) 同名的静态库.链接器在 v1.57 之前找到了 v1.55 静态库,因此它使用这些库进行链接.但是在旧版本中,仍然没有引入函数boost::coroutines::stack_traits::default_size(),导致链接器找不到这个函数的正确位置,并返回undefind参考".

That turns out to be the old boost (v1.55) lib still existing, which has static libraries with the same name of my manually installed boost library (v1.57). The linker found the v1.55 static libs before v1.57, so it used these libs for linking. However, in the old version, the function boost::coroutines::stack_traits::default_size() is still not introduced, which makes the linker cannot find the right location of this function, and returns "undefind reference".

对于那些碰巧在编译过程中通过谷歌搜索未定义引用"错误发现这个问题的人,我有几个关于调试此类问题的步骤,并写在这里供您参考.

For those who happens to find this questions from googling "undefined reference" errors during compiling, I got several steps about debugging this kind of problem and wrote here for your information.

  1. 检查你链接的库是否存在于/usr/lib//usr/local/lib/,如果没有,尝试安装这些首先.

  1. Check if the library you linked with exists in /usr/lib/ or /usr/local/lib/, if not, try to install these first.

检查您是否为引用的库添加了正确的链接标志.例如,如果您使用 boost_coroutine 库,请务必在链接期间添加 -lboost_coroutine.

Check if you added the correct linking flag for your referred libs. For example, if you use boost_coroutine libs, be sure to add -lboost_coroutine during linking.

检查链接标志的顺序是否正确(对于 gcc).这可能是大多数情况的原因.如果 B 依赖于 A,请确保将 B 放在 A 前面.

Check if the order of linking flags are correct (for gcc). This could be the cause of most cases. Make sure to put B in front of A if B depends on A.

检查相同库是否存在不同版本.

Check if different versions exists for the same libraries.

我希望这些能有所帮助.

I hope these could help somehow.

这篇关于链接错误“未定义的引用";关于编译过程中的boost静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆