12

I have the following CMake file:

project(MyLib)
cmake_minimum_required(VERSION 2.8)

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "release")
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Werror")
set(ROOT_DIR ${CMAKE_SOURCE_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ROOT_DIR}/bin/${CMAKE_BUILD_TYPE})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ROOT_DIR}/bin/${CMAKE_BUILD_TYPE})
set(MAIN_LIBRARY_NAME "mylib")

add_subdirectory(src)
add_subdirectory(test_app)
add_subdirectory(test_app1) <--- I want to disable -Werror flag for CMakeLists.txt in this folder.
add_subdirectory(test_app2)

How to disable -Werror flag for one of subdirectories? In the each of sub directories I have CMakeLists.txt too.

Volodymyr Hnatiuk
  • 187
  • 1
  • 1
  • 11
  • 1
    All variables and directory properties are copied to the subdirectory's context at the moment you call `add_subdirectory()`. Either modify `CMAKE_CXX_FLAGS` before this call or use [`add_compile_options()`](https://cmake.org/cmake/help/latest/command/add_compile_options.html) and modify [`COMPILE_OPTIONS`](https://cmake.org/cmake/help/latest/prop_dir/COMPILE_OPTIONS.html) sub-directory property after this call. – Florian Dec 16 '16 at 11:35
  • @Florian Not exactly a duplicate I would say. The answer to this question is contained in that question, not in one of its answers. – Antonio Dec 16 '16 at 13:22
  • @Antonio So I need to do like in example in this question: stackoverflow.com/questions/33828855/… ? – Volodymyr Hnatiuk Dec 16 '16 at 17:29
  • @Antonio Or I can add some compile options? As far I understand I can use add_compile_options() after add_subdirectory(test_app1) , or am I mistaken? – Volodymyr Hnatiuk Dec 16 '16 at 17:31
  • @VolodymyrHnatiuk You should put `add_compile_options` in the subdirectory CMakeLists.txt file, at the beginning. All its subsubdirectory will inherit the options. – Antonio Dec 16 '16 at 21:42

3 Answers3

11

Turning my comment into an answer

All variables and directory properties are copied to the subdirectory's context at the moment you call add_subdirectory(). Either modify CMAKE_CXX_FLAGS before this call with something like

string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
add_subdirectory(test_app1) 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")

Or use the "old way" were compiler flags could be given with add_definitions() and removed with remove_definitions()

add_definitions(-std=c++11 -Wall -Werror)
...
remove_definitions(-Werror)
add_subdirectory(test_app1)
add_definitions(-Werror)

But you can - in contradiction to my first comment - change flags added add_compile_options() only on target level COMPILE_OPTIONS property and not for complete directories. When the target is created the property is copied as-is from the directory to the target and changing the directory property later won't automatically update the target's property.

If you have a newer CMake version that supports SOURCE_DIR target property you can alternatively go by some crazy generator expression:

add_compile_options(
    -std=c++11 
    -Wall 
    $<$<NOT:$<STREQUAL:$<TARGET_PROPERTY:SOURCE_DIR>,${CMAKE_SOURCE_DIR}/test_app1>>:-Werror>
)

Reference

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
1

If you are using CMake v3.24's new CMAKE_COMPILE_WARNING_AS_ERROR variable, which initializes the corresponding target property, you can simply set the variable to FALSE in the CMakeLists.txt file of any subdirectories where you won't want warnings to be errors.

There are two nice things about using this variable:

  • Cross-platform with less boilerplate: No more explicitly written generator expressions to use the right flag for each compiler.
  • Allows user-override: Not all users will want to build with warnings as errors. This new feature comes with a --compile-no-warning-as-error command-line flag that users can use to disable any effects of this variable/target-property when set by a dev in a CMakeLists.txt file.
starball
  • 20,030
  • 7
  • 43
  • 238
1

we have a project setup which configured Werror and Wall at the top level as compiler flags. What I ended up doing for a temporary test program which I wanted to skip Werror was:

add_executable(test_program ...)
target_compile_options(test_program PRIVATE -Wno-error)

that ended up generating a compile line like:

g++ ... -Wall -Werror -Wno-error test.cpp -o test

where the -Wno-error cancels out the -Werror, without affecting the CXXFLAGS for the rest of the project.

rrosa
  • 368
  • 4
  • 9