Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c++ - 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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...