Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CMake how do I make `TARGET_LINK_LIBRARIES` suppress warnings from 3rd party library code?

In CMake, you can make TARGET_INCLUDE_DIRECTORIES() add include directories as system include directories (i.e. use -isystem) in order to not let warnings pop up which have their root in 3rd party code:

TARGET_INCLUDE_DIRECTORIES(mytarget
    SYSTEM
        ${3rdPartyLib_INCLUDE_DIR})

I prefer to use TARGET_LINK_LIBRARIES which also makes include directories from 3rd party libraries available. As far as I know, TARGET_LINK_LIBRARIES does not support the SYSTEM modifier to add those directories as a system include directories.

Did I get something wrong?

Is there a way to make:

TARGET_LINK_LIBRARIES(mytarget
    ${3rdPartyLib_INCLUDE_DIR})

use -isystem? (or any other way to suppress warnings from 3rdPartyLib).

like image 573
frans Avatar asked Aug 13 '18 07:08

frans


People also ask

How do you stop warnings in Cmake?

cmake:48 (include) CMakeLists. txt:208 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. You can disable the warning like this when you are configuring your build.

How do I turn off GCC warning?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.


1 Answers

I had a similar question, which I solved with a custom function:

function(target_link_libraries_system target)
  set(libs ${ARGN})
  foreach(lib ${libs})
    get_target_property(lib_include_dirs ${lib} INTERFACE_INCLUDE_DIRECTORIES)
    target_include_directories(${target} SYSTEM PRIVATE ${lib_include_dirs})
    target_link_libraries(${target} ${lib})
  endforeach(lib)
endfunction(target_link_libraries_system)

I can now call target_link_libraries_system(myapp lib::lib) and the include directories are read from the target's properties.

This could now be extended to specify the PUBLIC|PRIVATE|INTERFACE scope but since I use it on an executable, this is sufficient for now.

like image 59
sebastian Avatar answered Sep 28 '22 10:09

sebastian