CMake boost imported targets

家住魔仙堡 提交于 2020-01-24 21:34:08

问题


Can you explain me how should I use something like Boost::headers in my CMake file? I tried to utilize it somehow but is seems to work in different manner than I understand it.

cmake_minimum_required(VERSION 3.15)

project(Test)

find_package(Boost COMPONENTS asio REQUIRED)

add_executable(test main.cpp)

target_include_directories(test SYSTEM PUBLIC ${Boost_INCLUDE_DIR})
target_link_libraries(test PUBLIC Boost::headers)

This example does not work. It is caused by COMPONENTS asio. ASIO seems to be treated in other way because it is header only library.

Documentation says:

Boost::[C] - Target for specific component dependency (shared or static library)

[C] is lower-case

I can agree that ASIO may not be library in the terms of CMake's definition. Documentation emphasises that it must be either static or shared library. ASIO is header only so it is just a file to be included.

So let's try to use it in another way:

cmake_minimum_required(VERSION 3.15)

project(Test)

find_package(Boost REQUIRED)

add_executable(test main.cpp)

target_include_directories(test SYSTEM PUBLIC ${Boost_INCLUDE_DIR})
target_link_libraries(test PUBLIC Boost::headers)

Right now I am assuming that if CMake managed to find Boost, ASIO will be present. It is possible to perform configuration step, but when I try to build whole project it turns out that I am missing some dependencies. Boost::headers seems to be somehow unnecessary.

Unfortunately CMake's documentation is not the best I have ever seen. It is hard to get any information related with more complicated things.


回答1:


The target to include the Boost headers is named Boost::boost not Boost::headers.

If you use that, you don't need target_include_directories(... ${Boost_INCLUDE_DIRS}) ( the note S at the end of the variable). ${Boost_INCLUDE_DIR} without S is used in find_package to lookup the header files.

Boost::asio is a header only library and CMakes FindBoost.cmake module does not create targets for header only libraries. When dealing with Boost::asio you need to add system and thread to your required modules in find_package and add Boost::thread and Boost::system to your target_link_libraries call. See https://think-async.com/Asio/AsioAndBoostAsio.html for a good explanation.

So your CMakeLists.txt would look like follows:

cmake_minimum_required(VERSION 3.15)

project(Test)

find_package(Boost COMPONENTS system thread REQUIRED)

add_executable(test main.cpp)

target_link_libraries(test PUBLIC Boost::boost Boost::system Boost::thread)

Edit: Boost::headers was introduced in CMake 3.15 and Boost::boost is now an alias to it.



来源:https://stackoverflow.com/questions/58203003/cmake-boost-imported-targets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!