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

纵然是瞬间 提交于 2019-12-23 02:33:07

问题


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

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

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

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

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?


回答1:


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). 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. Check if the library you linked with exists in /usr/lib/ or /usr/local/lib/, if not, try to install these first.

  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.

  3. 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.

  4. Check if different versions exists for the same libraries.

I hope these could help somehow.



来源:https://stackoverflow.com/questions/34031869/linking-error-undefined-reference-about-boost-static-library-during-compiling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!