2017-08-28 37 views
1

我已經使用這個命令如何在使用CMake的ubuntu上找到已安裝的Boost庫?

sudo apt-get install libboost-all-dev 

安裝升壓和我在main.cpp中

#include <iostream> 
#include <boost/asio.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 

int main() 
{ 
    boost::asio::io_service io; 

    boost::asio::deadline_timer t(io, boost::posix_time::seconds(5)); 
    t.wait(); 

    std::cout << "Hello, world!" << std::endl; 

    return 0; 
} 

寫了這個簡單的例子,在我的CMakeLists.txt我有這樣的:

cmake_minimum_required(VERSION 2.8) 

find_package(Boost REQUIRED) 
if(NOT Boost_FOUND) 
    message(SEND_ERROR "Failed to find Boost") 
    return() 
else() 
    include_directories(${Boost_INCLUDE_DIR}) 
endif() 

add_executable(main main.cpp) 

CMake正常工作,但啓動後使我有一些錯誤:

main.cpp:(.text+0x11f): undefined reference to `boost::system::generic_category()' 

如何在我的CMakeLists.txt中正確包含boost以便cmake能夠找到庫?

+0

確保您的CMake版本足夠新:https://stackoverflow.com/a/42124857/2799037 – usr1234567

回答

0
main.cpp:(.text+0x11f): undefined reference to `boost::system::generic_category()' 

它在鏈接步驟失敗。您沒有鏈接到系統庫。你需要這樣做。

對於使用boost的CMake,您沒有遇到任何錯誤。你只需要告訴它系統需要被鏈接。

1

你需要鏈接到boost庫。 FindBoost提供可變Boost_LIBRARIES此:

add_executable(main main.cpp) 
target_link_libraries(main ${Boost_LIBRARIES}) 

欲瞭解更多信息,請參閱the FindBoost documentation。最後有一個例子。

0

要添加到以前的答案,here是您需要鏈接的Boost庫的列表。 (自Boost 1.65開始)

所有其他升壓庫都可以通過簡單地包含標頭來使用。