[CMake] [EXT] How to properly add include directories?

Bon, William william.bon at caldera.com
Mon Oct 21 10:18:40 EDT 2019


Hello,

Have you try to add the header files in the add_library command to explicitely add a dependency over it ?

add_library(MyLib SHARED my_source.cpp ../MyHeaders/header.hpp etc.)

On 10/21/19 4:16 PM, David Aldrich wrote:
WARNING: External email, please be mindful before clicking or replying.

Hi again,

My CMakeLists.txt file for my shared library contains:

add_library(MyLib SHARED my_source.cpp etc.)
target_include_directories( MyLib PRIVATE ../MyHeaders)

The library builds ok, but there is no dependency on directory ../MyHeaders - touching a header file does not result in a re-compile of dependent source files.

There's a discussion here:

https://stackoverflow.com/questions/13703647/how-to-properly-add-include-directories-with-cmake<https://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.com%2Fv3%2F__https%3A%2F%2Fstackoverflow.com%2Fquestions%2F13703647%2Fhow-to-properly-add-include-directories-with-cmake__%3B!NS9wnPUNuDs!whDhrk7RGTn3RadMaGwNJ7gyGUJg_fOCmaUVKr9T3ock9JmdX37y1aDQW7XthkCVdA%24&data=01%7C01%7Cwilliam.bon%40caldera.com%7C2e78ae5316384a4a4aa908d756315037%7C3d2d2b6f061a48b6b4b39312d687e3a1%7C1&sdata=zpBAH%2BeG3WhFkfgnzt9iTMsiK0NBupnbI2fTjvoK3TM%3D&reserved=0>
Opinion seems divided over whether or not it is necessary to add the header files to the list of source files for the target, e.g.:

set(SOURCES file.cpp file2.cpp ${YOUR_DIRECTORY}/file1.h ${YOUR_DIRECTORY}/file2.h)
add_library(test ${SOURCES})

Please will you tell me what is the best practice?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20191021/fba8d7ba/attachment.html>


More information about the CMake mailing list