简体   繁体   English

在编译过程中链接有关boost静态库的错误“ undefined reference”

[英]Linking error “undefined reference” about boost static library during compiling

I am trying to compile my C++ project on Linux x64 with boost libraries 1.57. 我正在尝试使用Boost库1.57在Linux x64上编译我的C ++项目。

This project is compiled with scons and I successfully compiled it on my Arch Linux, but this time failed on a Ubuntu machine. 该项目使用scons编译,我在Arch Linux上成功编译了该项目,但是这次在Ubuntu计算机上失败了。

I added -lboost_coroutine flag for linking but the error "undefined reference" still existed. 我添加了-lboost_coroutine标志以进行链接,但错误“未定义的引用”仍然存在。

/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()'

I noticed that the attributes.hpp is exactly one of the boost coroutine header files. 我注意到attributes.hpp正是增强协程头文件之一。 And I tried with nm to boost coroutine libs and it seems no problem. 我尝试使用nm来增强协程库,这似乎没有问题。

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

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. 在这种情况下,rpcx.o取决于boost_coroutine,因此它出现在前面。

What else could be the causes? 还有什么可能的原因?

After several hours effort, I finally solved it. 经过几个小时的努力,我终于解决了。

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). 原来那是仍然存在的旧boost(v1.55)库,它具有与我手动安装的boost库(v1.57)相同名称的静态库。 The linker found the v1.55 static libs before v1.57, so it used these libs for linking. 链接器在v1.57之前找到了v1.55静态库,因此它使用这些库进行链接。 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". 但是,在旧版本中,仍未引入函数boost::coroutines::stack_traits::default_size() ,这使链接器无法找到此函数的正确位置,并返回“未定义引用”。

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. Check if the library you linked with exists in /usr/lib/ or /usr/local/lib/ , if not, try to install these first. 检查链接的库是否在/usr/lib//usr/local/lib/ ,如果不存在,请尝试首先安装它们。

  2. 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. 例如,如果您使用boost_coroutine库,请确保在链接期间添加-lboost_coroutine

  3. Check if the order of linking flags are correct (for gcc). 检查链接标志的顺序是否正确(对于gcc)。 This could be the cause of most cases. 这可能是大多数情况下的原因。 Make sure to put B in front of A if B depends on A. 如果B依赖于A,请确保将B放在A的前面。

  4. Check if different versions exists for the same libraries. 检查同一库是否存在不同版本。

I hope these could help somehow. 我希望这些可以有所帮助。

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

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