简体   繁体   中英

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?

Converting my comment to an answer and further elaborating some details

It's handy to use the top-level directory (ie 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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM