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

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

问题描述

我正在尝试使用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正是增强协程头文件之一.而且我尝试使用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.

还有什么可能的原因?

What else could be the causes?

推荐答案

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

After several hours effort, I finally solved it.

原来是仍然存在的旧boost(v1.55)库,它具有与我手动安装的boost库(v1.57)相同名称的静态库.链接器在v1.57之前找到了v1.55静态库,因此它使用这些库进行链接.但是,在旧版本中,功能boost::coroutines::stack_traits::default_size()仍未引入,这使得链接器无法找到该功能的正确位置,并返回"undefind reference".

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.

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

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