2015-10-12 44 views
2

当我尝试编译我的CMake的项目,该项目采用升压和ASIO,与make我得到这些错误:找不到以下Boost库:boost_asio

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。

+0

你编译库或可执行文件?这很重要,因为如何正确使用asio,必须配置boost才能更改是否将asio构建到lib中。 –

回答

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

我添加了这些行:'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})'在可执行文件的目录中。问题仍然存在 – EMBLEM

+0

在您的'target_include_directories'调用中,'PUBLIC'必须位于'$ {Boost_INCLUDE_DIRS}'之前 - 但这不是问题。你现在正在做'find_package(Boost 1.54.0 REQUIRED)',还是你指定了单独的Boost组件?你在调用'target_link_libraries'之前调用'find_package(Boost ...)'*(试着在'target_link_libraries'调用确认前打印'$ {Boost_SYSTEM_LIBRARY}'的值)。 – Fraser

+1

除非另行配置,否则:: asio显式依赖boost :: system,因为它使用boost :: system中定义的错误代码/结构。所以这至少有一个问题,是你需要链接到:: system。 –