feelfree
feelfree

Reputation: 11763

Why will I have C++ unknown reference link error on the application program but not the library in linux

Now I am building a C++ dynamic library libabc.so and an application test based on this library in linux. libabc.so will invoke boost dynamic library libboost.so. I can compile libabc.so very well, and no errors can be found. If I use ldd command on libabc.so, I can easily observe that this library has a dependency on libboost.so. However, when I compile the application program test, I have the following link error:

abc.so: undefined reference to `boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*)'
collect2: ld returned 1 exit status

I do not know where the problem comes from. When I compile test program, I am sure that I link it with both libabc.so and libboost.so. I also changed the sequence of libabc.so and libboost.so when linking just to make sure that the right library sequence is given. Any ideas? Thanks.

Upvotes: 0

Views: 501

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 120031

The function in question is defined to take different arguments, depending on whether the programis compiled as C++03 or C++11.

void boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option::enum_type, boost::system::error_code*) // pre-C++11

void boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*) // C++11

In my opinion this is a Boost bug.

Consequently, if this function is used in a program, then both Boost and the program must be compiled with C++11 support, or both without. Otherwise the program will fail at the linking stage.

Shared libraries in Linux are normally allowed to have unresolved references, this doesn't cause their linking to fail.

Upvotes: 2

Related Questions