When to use BUILD_INTERFACE and INSTALL_INTERFACE generator expressions with target_include_directories?

Viewed 999

The target_include_directories documentation states the following:

Include directories usage requirements commonly differ between the build-tree and the install-tree. The BUILD_INTERFACE and INSTALL_INTERFACE generator expressions can be used to describe separate usage requirements based on the usage location.

I would like to make my CMake library installable, and I'm not sure if that means I should use these generator expressions. Currently, my src/CMakeLists.txt contains the following:

target_include_directories(mylib PRIVATE ${CMAKE_SOURCE_DIR}/include/mylib)
target_include_directories(mylib INTERFACE ${CMAKE_SOURCE_DIR}/include)

This allows me to #include my library's public headers without specifying the parent directory while still requiring that users of my library do so themselves. My understanding is that this is standard practice for preventing header filename collisions.

That being said, I don't believe this code will work in its current form for installing the library. What is typically the preferred way to go about fixing this?

0 Answers
Related