1

i am using Jetbrains CLion 2017.3 and the bundled CMake version 3.9.6 with mingw64 5.0 version/g++ 7.1. Although reading the "Mastering CMake" ( i am new to CMake !) i have many difficulties to understand the basics. Since 3 days i am searching for a CMake solution to create a own header-only library that uses the boost (1.66.0 ) libraries.

Using my CMakeLists.txt results in finding the boost libraries, but i cannot include boost headers in a header file from my current source directory.

My current source diretory contains the "CMakeLists.txt" and the header file "test_boost.h". If i try to include boost headers in the header file "test_boost.h", boost headers cannot be found !

What i am doing wrong ?

My CMakeLists.txt :

cmake_minimum_required(VERSION 3.9)
project(headerOnlyLib1)

set(CMAKE_CXX_STANDARD 11)

set(ENV{BOOST_ROOT} "C:/dev/boost/mingw/boost_1_66_0/boost")
set(Boost_USE_STATIC_LIBS        ON) # only find static libs
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(BOOST 1.66 REQUIRED)
IF (Boost_FOUND)
    message(STATUS "BOOST FOUND !")
ELSE()
    message(STATUS "BOOST NOT Found !")
endif()
add_library(headerOnlyLib INTERFACE)
target_include_directories(headerOnlyLib INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(headerOnlyLib INTERFACE ${Boost_INCLUDE_DIRS})
target_link_libraries(headerOnlyLib ${Boost_LIBRARIES})
user3664264
  • 31
  • 1
  • 3

2 Answers2

1

Short answer: You can't.

A "header-only library" is just that, one or more headers, only. It's not something that is linked or really stand-alone.

If your header-only library have dependencies, then the users of your library also have those dependencies and need to include them in their own build.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • i think your answer means that,in the case of header-only libraries, the are no binaries involved. That i have understood. But is there no possibility in CMake to include other library header files ( here boost header files) in own header files belonging to the own header-only library? – user3664264 Mar 25 '18 at 12:25
  • 1
    @user3664264 No that's not really how header file works. Well you *can* use the preprocessor on your header file, which will create a really big header file that includes all headers you include. It's usually not a good idea, since your code becomes obfuscated and hard to find. It might also be problem with licensing. So really, distribute your library as a standalone set of header files, then have Boost as an explicit dependency that others need to use. – Some programmer dude Mar 25 '18 at 12:38
  • Thank you for your clarification. i think now i have understood your previous answer. – user3664264 Mar 25 '18 at 13:12
0

I think that you can, but you need to be more specific in defining your boost dependencies.

For example, the CMakeLists.txt file here depends upon boost::system for boost::asio. The dependency is defined as follows:

find_package(Boost REQUIRED COMPONENTS system)
if(Boost_FOUND)
  target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS})

  # Boost::asio is header only but it requires Boost::system
  target_link_libraries(${PROJECT_NAME} INTERFACE Boost::system)
.
.
.
endif(Boost_FOUND)

In your case, the target is Boost::boost for header-only dependencies, see FindBoost. So the relevant part becomes:

find_package(Boost 1.66 REQUIRED COMPONENTS boost)
IF (Boost_FOUND)
    message(STATUS "BOOST FOUND !")
    target_include_directories(headerOnlyLib INTERFACE ${Boost_INCLUDE_DIRS})
ELSE()
    message(STATUS "BOOST NOT Found !")
endif()

I recommend watching Daniel Pfeifer's talk at C++ Now 2017 for more information.
A lot has changed since "Mastering CMake" was written...

kenba
  • 4,303
  • 1
  • 23
  • 40