Derongan
Derongan

Reputation: 810

CMake Include third party header for a non cmake library

I have a project set up like so:

project
  CMakeLists.txt
  src
    CMakeLists.txt
    Mylib.cpp
  thirdparty
    somelib
      include
        header.h

I am trying to include header.h inside my code with #include "header.h" however the preprocessor cannot find the header.

I have tried adding the third party include directory using target_include_directories in the src CMakeLists.txt, however that does not help. I have tried both thirdparty/somelib/include and ../thirdparty/somelib/include for the path.

How do I properly get the header on the include path?

Upvotes: 0

Views: 2252

Answers (1)

compor
compor

Reputation: 2339

Converting my comment to an answer and further elaborating some details

It's handy to use the top-level directory (i.e. root dir) of a project tree, which also has the advantage of shielding you from any locations changes that might take place on the dependent targets

target_include_directories(foo PUBLIC ${CMAKE_SOURCE_DIR}/thirdparty/somelib/include)

Make sure to adjust visibility as required (see docs). Moreover, if you are planning to install your targets you should adjust the location of the installed headers differently from those used during a build (especially from the user-facing headers of your API), using generator expressions like this

target_include_directories(foo PUBLIC
  $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/thiirdpart/somelib/include/>
  $<INSTALL_INTERFACE:include/thirdparty/somelib/include>  # or whichever structure you choose
)

Upvotes: 1

Related Questions