2015-10-12 46 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。 –