Home > Net >  How to set/unset compiler flags in a cmake project?
How to set/unset compiler flags in a cmake project?

Time:09-27

I am new to cmake and made a cmake project with scaffolding provided by qt creator. I added a library (assimp) in source form. While compiling my project with the default kit (mingw), I get errors that all have the following:

error: ignoring '#pragma warning ' [-Werror=unknown-pragmas]

I understand that the flag "-Werror=unknown-pragmas" asks the compiler to treat unknown pragmas as errors. Assimp has many pragma directives that gcc doesn't understand so I would like to not pass that flag to the compiler. I looked in settings and can't find where the flag is set. How do I disable it so that my program compiles?

[edit]: I searched cmake files of Assimp library and couldn't find culprit compiler flag. It makes me think it has to do with what qt passes to cmake when invoking it. In the Projects->Build Settings->Cmake->Initial Configuration, I found:

-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}

What does this evaluate to?

CodePudding user response:

I personally add the compile flags for a specific target via target_compile_options. For instance:

target_compile_options(my_app PRIVATE -Werror)

Notice:

  1. you need a target, created via add_library or add_executable, and that
  2. you can also use INTERFACE or PUBLIC instead of PRIVATE. If it's a "final" target you don't plan to link against anything else, PRIVATE should be the way to go.

Now, if you are linking against a library, e.g. assimp, and you are getting compiler errors coming from that library, I think what you want is to use SYSTEM when adding that library to your target's include directories:

target_include_directories(my_app SYSTEM PRIVATE ${ASSIMP_INCLUDE_DIR})

CodePudding user response:

Removing/Unsetting Cmake flags

The best option I could think of would be to edit the cmake file of assimp, removing the -Wall and other similar flags.

I would also look at this question: CMake - remove a compile flag for a single translation unit

Setting Cmake flags

Though this does not help with unsetting flags, you can set C compiler flags using the set function:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Llib -lwhatever")

With the above flags replaced with yours.

As Marek K said in the comment, it is considered bad practice to set global flags, you should instead apply flags for each target.

  • Related