How to add a header only library to a project with cmake build system?

1.8k Views Asked by At

I need to modify a C++ project to use Cereal library. The build system of this project is based on CMake. Since Cereal is a header only library and also uses CMake, I expect this to be a pretty simple task. I tried editing the CMakeLists.txt with:

include(ExternalProject)
ExternalProject_Add(cereal
GIT_REPOSITORY    [email protected]:USCiLab/cereal.git
CMAKE_ARGS -DJUST_INSTALL_CEREAL=ON -DSKIP_PORTABILITY_TEST=ON -DBUILD_TESTS=OFF
PREFIX ${CMAKE_INSTALL_PREFIX})

but somehow cmake tries to install cereal under /usr/local. I appreciate any help that can point me in the right direction.

2

There are 2 best solutions below

0
On

I am not sure if this is the best thing to do, but the following works. The trick is not to install anything and set include directory correctly.

include(ExternalProject)
ExternalProject_Add(cereal
  GIT_REPOSITORY    [email protected]:USCiLab/cereal.git
  PREFIX cereal
  SOURCE_DIR "${CMAKE_BINARY_DIR}/third-party/cereal"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND ""
  INSTALL_COMMAND ""
)
include_directories(
    ${CMAKE_BINARY_DIR}/third-party/cereal/include) 
6
On

The proper way is always to link to the library:

target_link_libraries(your-executable PUBLIC cereal::cereal)

Since this library define a target, you can just use it so the include directories are automatically set to your target.