[CMake] Use interface libraries for providing compile options and definitions

Dustyn Blasig dustyn at blasig.us
Thu Oct 3 12:26:45 EDT 2019


Hi All,

I have been cleaning up our legacy CMake to use newer features (available
in 3.12+) including trying to use target_...() functions nearly
exclusively. As part of this, I was toying with cleaning up our use cases
for adding compiler flags and similar definitions using real targets and
target_link_libraries.

For instance, as a simple example, let's say I wanted to add/provide a
definition MY_FLAG, I could do something like...

```
add_library(my_flag INTERFACE)
target_compile_definitions(my_flag INTERFACE MY_FLAG=1)

add_library(other_library SHARED ...)
target_link_libraries(other_library ... *PRIVATE *my_flag)

export/install rules
```

I want this library to be private to my component, and it's only used under
the PRIVATE banner. However, the issue I'm running into is with the
install/export rules. I get an error similar to ...

```
CMake Error: install(EXPORT "MY_PROJECT" ...) includes target
"other_library" which requires target "my_flag" that is not in the export
set.
```

If my_flag is defined in my component, I can add it to the export set
perhaps to workaround the issue, but in many cases, it would be coming from
a helper script in another sub-project I'm fetching using FetchContent and
don't want to expose the functionality via my export scripts.

(1) Is it recommended to use interface libraries to clean up compile
defintions, etc.
(2) Should it be possible to link privately such libraries and not have the
export functionality complain?

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20191003/1a363dd0/attachment.html>


More information about the CMake mailing list