1

A user of my project reported this error to me. I cannot reproduce it on my computer or my lab's server, so I ask it here.

The project uses CMake to generate build environment. It uses the FindBoost utility (provided with CMake) to find Boost resources.

On the beginning, my user said while linking the final programs, the compiler was provided with "/usr/lib64/lib64/libboost_XXX.so", instead of the correct "/usr/lib64/libboost_XXX.so". I failed to find why CMake generated such weird library location, and asked him to manually set the variable Boost_LIBRARIES, and print them:

Boost libraries are: /usr/lib64/libboost_thread-mt.so;/usr/lib64/libboost_program_options-mt.so;/usr/lib64/libboost_filesystem-mt.so

Things seem to be correct. The compilation was successful. But when it goes to linking, the program cries about many undefined symbols:

CMakeFiles/ht-filter.dir/ht-filter.cpp.o: In function `parse_options(int, char**)':
/public/home/yli/Downloads/htqc-0.15.0-Source/ht-filter.cpp:43: undefined reference to `boost::program_options::options_description::options_description(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, unsigned int)'
......
/usr/local/include/boost/program_options/errors.hpp:372: undefined reference to `boost::program_options::validation_error::get_template(boost::program_options::validation_error::kind_t)'

This is the two typical of the so-many errors: one locates from my source code, the other locates from boost's header. In the corresponding line of my source code, I just created the options_description object

// I renamed boost::program_options to opt
opt::options_description opt_main("Options:");

The OS of my user is CentOS 6.2, and his Boost version is 1.50.0 which is similiar with the one in my computer. The version of CMake of my user is 2.8.11 which is also same with mine.

4

1 回答 1

2

在使用 CMake 的 find_package 进行 Boost 时,您可以给 CMake 一些提示,这可能有助于它找到正确的库,例如:

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

set(BOOST_ROOT "/usr")

find_package(Boost 1.50.0)

message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")

另外,如果您要与动态库链接,请确保让 Boost 标头知道它(我认为您不应该在此处弄乱顺序):

link_directories(${Boost_LIBRARY_DIRS})

include_directories(${Boost_INCLUDE_DIRS})

add_definitions( -DBOOST_ALL_DYN_LINK )
于 2013-08-08T08:21:24.017 回答