How to limit the scope of compiler flags only to a (my) local library?


I am trying the example below :


        PUBLIC include ${catkin_INCLUDE_DIRS} ${thirdPartyLib_INCLUDE_DIRS})


        ${thirdPartyLib_LIBRARY} ${ ${catkin_LIBRARIES})

target_compile_options(mylib PRIVATE -Werror -Wall -Wextra)

The issue is that the compile options also propagate to thirdPartyLib , yet I need them only for mylib .

Try adding the thirdPartyLib_INCLUDE_DIRS as SYSTEM. That should silence the warnings/errors those headers may cause.

Isn’t add_dependencies causing that behavior? Did you try to create a separate target for the third party lib (so it compiles with default flags) and then link it with your main lib using target_link_libraries?

Did not solve the issue.

Your example is incomplete. How is created thirdPartyLib? Are you sure same compile flags are not also defined for this target?

It is found with

Is it a module delivered as part of CMake? If yes, please give the information.
If no, how is consumed this library? through a imported target or not?
Did you check that these flags are not defined elsewhere (globally or at directory level using command add_compile_options or variable CMAKE_<LANG>_FLAGS)?

Currently, without a complete environment description, it is impossible to identify where come from your problem.

thirdPartyLib is a custom lib not part of CMake. The compilation does not break if I remove my compile flags. Then the issue is at my CMake file.