0

I have an external library which I'm using in my package

- package/
  - include/
  - src/
  - external_library/
    - CMakeLists.txt
  - CMakeLists.txt

and currently in package/CMakeLists.txt I add the external library by

add_subdirectory(external_library library_name)
add_executable(my_executable src/main.cpp)
target_link_libraries(my_executable library_name)

and within external_library/CMakeLists.txt there is something like

add_library(library_name <files>)

I'd like to disable warnings from files in external_library/ so I can change the error settings in package/CMakeLists.txt without seeing conflicts from the external library. I would like to avoid changing anything in external_library/ if at all possible. How can that be done?

starball
  • 20,030
  • 7
  • 43
  • 238
Morten Nissov
  • 392
  • 3
  • 13
  • Related, (but not a dup of): [How to selectively enable or disable -Werror argument for entire (external project) directories in my project?](https://stackoverflow.com/q/73384774/11107541) – starball Apr 28 '23 at 11:08

2 Answers2

1

You could manually add the -w flag for each external target, which disables all warnings on GCC, Clang, and MSVC (or whatever flag for your compiler for a specific warning you want to disable), and rely on the fact that later flags will override previous flags for those compilers. Ex. To disable all warnings for the compilation of a target, you'd use target_compile_options(foo PRIVATE -w).

If there are a lot of targets in the external project and you want to suppress warnings for all of them in a similar fashion, assuming you don't want to modify the external project's CMakeLists.txt files, you can save a copy of the COMPILE_OPTIONS directory property, modify it, add the subdirectory, and then restore the old value. Ex.

# retrieve a copy of the current directory's `COMPILE_OPTIONS`
get_directory_property(old_dir_compile_options COMPILE_OPTIONS)

# modify the actual value of the current directory's `COMPILE_OPTIONS` (copy from above line remains unchanged). subdirectories inherit a copy of their parent's `COMPILE_OPTIONS` at the time the subdirectory is processed.
add_compile_options(-w)

# add you subdirectory (whichever way you do it)
# add_subdirectory(external ...)
# FetchContent_MakeAvailable(...)

# restore the current directory's old `COMPILE_OPTIONS`
set_directory_properties(PROPERTIES COMPILE_OPTIONS "${old_dir_compile_options}")

If the warnings are in the library's headers and they're happening when you include the headers, try marking the target as SYSTEM (which you can do with cmake_minimum_required(VERSION 3.25) or higher by using the SYSTEM target property). Pre-CMake 3.25, you can use a workaround in which you copy/move the INTERFACE_INCLUDE_DIRECTORIES target property to the INTERFACE_SYSTEM_INCLUDE_DIRECTORIES target property (related post). There may be other ramifications to doing this, but one of the side-effects of marking a target as SYSTEM is usually that the compiler will not emit warnings for them when #included by something that has warning flags.

If you have cmake_minimum_required(VERSION 3.25) or higher and the external library is header-only, you can consider a different solution. add_subdirectory and FetchContent_Declare come with a SYSTEM option that you can use, which causes all targets added in the corresponding subdirectory and under to have their include directories all treated as being SYSTEM (which will result in CMake generating a corresponding include flag for the compiler).

starball
  • 20,030
  • 7
  • 43
  • 238
  • the external library only has one target (the add_library target) but many files come together when it calls add_library – Morten Nissov Apr 28 '23 at 12:30
  • Setting `add_compile_options(-Wall -Wextra -Wpedantic)` in the beginning of the cmake, both the proposed strategies fail. Both meaning the `set_directory_properties` and `target_compile_options` – Morten Nissov Apr 28 '23 at 12:43
  • @MortenNissov https://chat.stackoverflow.com/rooms/253372/room-for-user-and-morten-nissov – starball Apr 28 '23 at 21:43
-1

Use the set_source_files_properties to set the COMPILE_FLAGS to ignore warnings (-w) for all source files of your external library.

# package/CMakeLists.txt 

# Get the list of all sources of external_lib
file(GLOB EXTERNAL_LIBRARY_SOURCES external_library/*.cpp)

set_source_files_properties(
    ${EXTERNAL_LIBRARY_SOURCES}
    PROPERTIES COMPILE_FLAGS "-w"
)

add_subdirectory(external_library library_name)
add_executable(my_executable src/main.cpp)
target_link_libraries(my_executable library_name)
Jishan Shaikh
  • 1,572
  • 2
  • 13
  • 31
  • 1
    isn't glob generally considered to be bad practice for modern cmake? – Morten Nissov Apr 28 '23 at 10:59
  • 1
    This is one way to get all the source files of the external library. Otherwise, you would have to list each source file of the external library that generates warnings individually. While GLOB is considered bad practice because it does not handle changes in the file system, in our case, as you have stated, the external library is unchanged, so it should not be affected. – Jishan Shaikh Apr 28 '23 at 11:03