6

I am editing a CMakeLists.txt file made by someone else. I'm trying to get rid of some of the warnings generated when compiling the project.

Normally I just add set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MY_FLAGS}") with whatever flags I need to add, and it works fine, but for this project, it's just not working. The warnings still appear. I tried a couple alternative methods, but nothing.

What could be causing the issue?

cmake_minimum_required(VERSION 3.1)
project(PBS)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

option(LIBIGL_USE_STATIC_LIBRARY "Use libigl as static library" OFF)
option(LIBIGL_WITH_ANTTWEAKBAR       "Use AntTweakBar"    OFF)
option(LIBIGL_WITH_CGAL              "Use CGAL"           OFF)
option(LIBIGL_WITH_COMISO            "Use CoMiso"         OFF)
option(LIBIGL_WITH_CORK              "Use Cork"           OFF)
option(LIBIGL_WITH_EMBREE            "Use Embree"         OFF)
option(LIBIGL_WITH_LIM               "Use LIM"            OFF)
option(LIBIGL_WITH_MATLAB            "Use Matlab"         OFF)
option(LIBIGL_WITH_MOSEK             "Use MOSEK"          OFF)
option(LIBIGL_WITH_OPENGL            "Use OpenGL"         ON)
option(LIBIGL_WITH_OPENGL_GLFW       "Use GLFW"           ON)
option(LIBIGL_WITH_OPENGL_GLFW_IMGUI "Use ImGui"          ON)
option(LIBIGL_WITH_PNG               "Use PNG"            OFF)
option(LIBIGL_WITH_PYTHON            "Use Python"         OFF)
option(LIBIGL_WITH_TETGEN            "Use Tetgen"         OFF)
option(LIBIGL_WITH_TRIANGLE          "Use Triangle"       OFF)
option(LIBIGL_WITH_VIEWER            "Use OpenGL viewer"  ON)
option(LIBIGL_WITH_XML               "Use XML"            OFF)

if (NOT LIBIGL_FOUND)
    find_package(LIBIGL REQUIRED QUIET)
endif()

add_subdirectory(0_dummy)
add_subdirectory(1_cannonball)
add_subdirectory(2_spring)
add_subdirectory(3_spinning)
add_subdirectory(4_gyro)


# Custom commands
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )
add_compile_options ( -Wno-reorder )
add_definitions ( -Wno-unknown-pragmas )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-sign-compare")

EDIT: I found out that adding the flags in one of the subdirectories works for that subdirectory (e.g. in 3_spinning/CMakeLists.txt). Is there no way of setting the flags globally?

Sean Bone
  • 3,368
  • 7
  • 31
  • 47
  • 1
    You are trying to set 3 different compiler flags. Which exact setting doesn't work? (That is, which flags do you **want** to set for suppress warnings?) – Tsyvarev Oct 06 '18 at 18:33
  • @Tsyvarev all three. I am trying alternate methods and am setting a different flag to see which one works, but none of them works. Eventually, I'll add all three. – Sean Bone Oct 07 '18 at 11:14
  • Ok, but you write "I found out that adding the flags in one of the subdirectories works for that subdirectory" - which way of setting flags **works** when used in a subdirectory? – Tsyvarev Oct 07 '18 at 15:28
  • @Tsyvarev All three methods work, I believe – Sean Bone Oct 12 '18 at 06:39

2 Answers2

2

Conclusion: add_compile_options and add_definitions work on the current directory and all included directories that are included after the command. Setting CMAKE_CXX_FLAGS, however, seems to only work on the current directory. Not sure why however, because as commenter Tsyvarev says, it should have the same scope as the first two methods.

Basically, shifting the lines around like this:

[...]
# Custom commands
add_compile_options ( -Wno-reorder )
add_definitions ( -Wno-unknown-pragmas )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-sign-compare")

add_subdirectory(0_dummy)
add_subdirectory(1_cannonball)
add_subdirectory(2_spring)
add_subdirectory(3_spinning)
add_subdirectory(4_gyro)

I no longer get -Wreorder and -Wunknown-pragmas warnings, but I still get -Wsign-compare warnings.

Sean Bone
  • 3,368
  • 7
  • 31
  • 47
  • 1
    Interesting. I guess I had the good intuition then. I think it is linked to the old cmake behavior, before the add_compile_option and other functions arrived. At the time, setting up the macros would impact all targets in the current folder, but not the subdirectories if defined after. I would also assume that using the functions is preferred to the macros in modern cmake. – Matthieu Brucher Oct 12 '18 at 08:55
1

You are adding the flags at the end, after scanning the subfolder, you have to first set the flags and then go through your subfolders.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • Indeed... In that case, which warnings are missing? Is it -Wno-sign-compare or -Wno-order or both? – Matthieu Brucher Oct 06 '18 at 17:30
  • Good question.. but it is better to address it to the asker :) – Tsyvarev Oct 06 '18 at 18:30
  • As @Tsyvarev said, setting `CMAKE_CXX_FLAGS` should work anyway, but it's not... None of the flags are being set. The only way I found was to set the flags within each subfolder separately. – Sean Bone Oct 07 '18 at 11:16
  • For the first flags, from add_*, they have to be defined before, not after the add_subdirectory. – Matthieu Brucher Oct 07 '18 at 11:20
  • Sorry, I was wrong about the effect of `CMAKE_CXX_FLAGS` variable. Its scope is same as for commands `add_compile_options` and `add_definitions`. That is, setting the variable affects on all targets in the current directory and on the targets in the subdirectories added *after* the variable's setting. So the answer is fully correct. @SeanBone: You need to set compiler flags **before** calls for `add_subdirectory`. In that case either of 3 methods should work. – Tsyvarev Oct 12 '18 at 06:51
  • 1
    @Tsyvarev this is what I just tried. Setting `CMAKE_CXX_FLAGS` before inclusion won't work, but the other two do indeed work. – Sean Bone Oct 12 '18 at 06:53