Kasata Ata
Kasata Ata

Reputation: 125

CMake's find_package with header only libraries

I have learned how to use find_package in CMake recently and I noticed that it works only with pre-compiled libraries.

This line works as expected:

find_package(Boost COMPONENTS filesystem REQUIRED)

If it is successful I can use something like that later:

target_link_libraries(target Boost::filesystem)

Using interfaces make it really easy to link all necessary dependendcies required by Boost.Filesystem. But it would be great to have similar solution for header-only libraries like Boost.Asio (I know that it is possible to build it into separate object but for example purposes I will treat it as header-only library)

find_package(Boost COMPONENTS asio REQUIRED)
target_link_libraries(target Boost::asio)

And this won't work as expected.

Implementation of ASIO requires us to link pthread on UNIX-like systems so we can say that this library has a dependency which is thread library. Is there any convinient solution to use it in the same manner as Boost.Filesystem in CMake?

Upvotes: 2

Views: 5029

Answers (2)

Rob Baily
Rob Baily

Reputation: 3019

I have been trying this recently with headers only Boost and found that this works for Asio as a header only library.

find_package(BOOST)
target_link_libraries(${PROJECT_NAME} Boost::headers pthread)

I found that despite it being header only if you do not include something in the target_link_libraries then the include directories are not automatically included. Don't know why that is the case but that is what I have seen.

Upvotes: 0

kenba
kenba

Reputation: 4549

You can use find_package to find header-only dependencies by not specifying a target, see FindBoost, i.e.:

find_package(Boost REQUIRED)
if(Boost_FOUND)
  include_directories(${Boost_INCLUDE_DIRS})
else()
  message(STATUS "Boost NOT Found !")
endif(Boost_FOUND)

However, you'll find that Boost::asio is header only but it requires Boost::system. Therefore, you require:

find_package(Boost COMPONENTS system REQUIRED)
if(Boost_FOUND)
  include_directories(${Boost_INCLUDE_DIRS})
  target_link_libraries(target Boost::system)
else()
  message(STATUS "Boost NOT Found !")
endif(Boost_FOUND)

Upvotes: 4

Related Questions