【问题标题】:Could not find the following Boost libraries: boost_asio找不到以下 Boost 库:boost_asio
【发布时间】:2015-10-12 22:47:28
【问题描述】:

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

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。

【问题讨论】:

  • 您是在编译库还是可执行文件?这非常重要,因为无论您是否将 asio 构建到 lib 中,您必须如何配置 boost 以正确使用 asio 都会发生变化。

标签: c++ boost cmake boost-asio


【解决方案1】:

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})

【讨论】:

  • 我在可执行文件的目录中添加了这些行:target_include_directories(client-network-handler-test ${Boost_INCLUDE_DIRS} PUBLIC) target_link_libraries(client-network-handler-test client-network-handler ${Boost_SYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_REGEX_LIBRARY})。问题依旧
  • 在您的target_include_directories 调用中,PUBLIC 必须位于${Boost_INCLUDE_DIRS} 之前——但这不是这里的问题。您现在是在做find_package(Boost 1.54.0 REQUIRED),还是在指定单个 Boost 组件?您是否在调用find_package(Boost ...)之前调用target_link_libraries(尝试在target_link_libraries 调用之前打印${Boost_SYSTEM_LIBRARY} 的值以确认)。
  • 除非另外配置,::asio 明确依赖于 boost::system,因为它使用 boost::system 中定义的错误代码/结构。所以这至少是一个问题,您需要链接到 ::system。
猜你喜欢
  • 2019-06-20
  • 2019-03-29
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-26
  • 1970-01-01
相关资源
最近更新 更多