2

I am trying the example below :

add_library(
        mylib
        src/my_code.cpp)

target_include_directories(mylib
        PUBLIC include ${catkin_INCLUDE_DIRS} ${thirdPartyLib_INCLUDE_DIRS})

add_dependencies(
        mylib
        ${mylib_EXPORTED_TARGETS}
        ${catkin_EXPORTED_TARGETS})


target_link_libraries(mylib
        PUBLIC
        ${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.

Courier
  • 920
  • 2
  • 11
  • 36
  • It is not possible that the compile options from `mylib` propagates to `thirdPartyLib` from what you have shown here. They must come from somewhere else. You don't have a `add_compile_options` with the same warning options set in a parent `CMakeLists.txt`? – arghol Jan 07 '20 at 10:19
  • If I comment `target_compile_options` then no issues – Courier Jan 07 '20 at 10:37
  • 1
    You have an extra `${ ` next to `${catkin_LIBRARIES}`. – Kevin Jan 07 '20 at 13:01

1 Answers1

1

I think that the problem is compiler warnings, which are generated by included thirdPartyLib header files when compiling file my_code.cpp.

If you want your compiler not to generate warnings from included third-party header files, you can for example in gcc/clang include them as "system headers" (command line option -isystem instead of -I).

To do this in CMake use SYSTEM option in target_include_directories:

target_include_directories(mylib
    SYSTEM
        PUBLIC ${thirdPartyLib_INCLUDE_DIRS}
)
kzeslaf
  • 261
  • 3
  • 6
  • Did not for me. Note that when I use `target_compile_options mylib INTERFACE -Werror -Wall -Wextra)` then no warning is considered. – Courier Jan 08 '20 at 08:28
  • Option `INTERFACE` means that code that links to your library (target `mylib`) will be compiled with options `-Werror ...` but not the code of your library. So by changing `PRIVATE` to `INTERFACE` you are turning off these options for `mylib`source files. – kzeslaf Jan 08 '20 at 10:22
  • If you use `make` to build your project you can run it as follows: `make VERBOSE=1` and inspect command line compiler options and see compilation of which files generates warnings. – kzeslaf Jan 08 '20 at 10:27
  • In fact I had to what you suggest to another depending custom lib. Thanks! Works now. – Courier Jan 08 '20 at 11:36