找不到以下Boost库:boost_asio

时间:2015-10-12 22:47:28

标签: c++ boost cmake boost-asio

当我尝试使用boost和asio编译我的cmake项目make时,我收到了以下错误:

CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `__cxx_global_var_init1':
/usr/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'    CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `__cxx_global_var_init1':
/usr/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `__cxx_global_var_init2':
/usr/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `__cxx_global_var_init3':
/usr/include/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `boost::asio::error::get_system_category()':
/usr/include/boost/asio/error.hpp:216: undefined reference to `boost::system::system_category()'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `__cxx_global_var_init1':
/usr/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `__cxx_global_var_init2':
/usr/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `__cxx_global_var_init3':
/usr/include/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `boost::network::uri::uri::parse()':
/home/darren/373project/include/boost/network/uri/uri.hpp:178: undefined reference to `boost::network::uri::detail::parse(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::network::uri::detail::uri_parts<__gnu_cxx::__normal_iterator<char const*, std::string> >&)'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `error_code':
/usr/include/boost/system/error_code.hpp:323: undefined reference to `boost::system::system_category()'
CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `__cxx_global_var_init2':
/usr/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `__cxx_global_var_init3':
/usr/include/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `boost::asio::error::get_system_category()':
/usr/include/boost/asio/error.hpp:216: undefined reference to `boost::system::system_category()'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `__cxx_global_var_init1':
/usr/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `__cxx_global_var_init2':
/usr/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `__cxx_global_var_init3':
/usr/include/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `boost::network::uri::uri::parse()':
/home/myUserName/373project/include/boost/network/uri/uri.hpp:178: undefined reference to `boost::network::uri::detail::parse(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::network::uri::detail::uri_parts<__gnu_cxx::__normal_iterator<char const*, std::string> >&)'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `error_code':
/usr/include/boost/system/error_code.hpp:323: undefined reference to `boost::system::system_category()'

我认为无法找到asio库。所以我将这一行添加到我的根CMakeLists.txt:

find_package(Boost 1.54.0 REQUIRED)

CMake可以找到所有相关的库,如构建日志的这一部分所示:

-- Found the following Boost libraries:
--   unit_test_framework
--   system
--   regex
--   date_time
--   thread
--   filesystem
--   program_options
--   chrono
--   atomic

但编译仍会产生列出的第一个错误。当我将CMake行更改为:

find_package(Boost 1.54.0 REQUIRED asio)

我从CMake收到此消息:

CMake Error at /usr/share/cmake-2.8/Modules/FindBoost.cmake:1131 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.54.0

  Boost include path: /usr/include

  Could not find the following Boost libraries:

          boost_asio

我已经安装了所有Boost库,如您所见,我无法理解为什么它找不到asio。

1 个答案:

答案 0 :(得分:4)

Boost.Asio是一个仅限标题的库 - 即它不需要链接到您的应用程序。

来自the docs for CMake's FindBoost module

  

此模块查找标题和请求的组件

(强调我的)。

换句话说,find_package(Boost ...)只应用于查找非标题的Boost库,如Boost docs中所列。

从您的链接器错误看,您似乎需要链接Boost.System:

find_package(Boost 1.54.0 REQUIRED system)
target_include_directories(MyExe PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(MyExe ${Boost_LIBRARIES})