0

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?

[edit]: I found a cache variable in Assimp that enables warnings as errors. Forgive me for not looking in well enough.

Revelant code in assimp/code/cmakelists.txt:

IF (ASSIMP_WARNINGS_AS_ERRORS)
  MESSAGE(STATUS "Treating all warnings as errors (for assimp library only)")
  IF (MSVC)
    TARGET_COMPILE_OPTIONS(assimp PRIVATE /W4 /WX)
  ELSE()
    TARGET_COMPILE_OPTIONS(assimp PRIVATE -Wall -Werror)
  ENDIF()
ENDIF()
  • you cannot find `unkown-pragmas` being set most likely because it isnt set explicitly. Its part of `-Wall -Werror` (and perhaps others) https://godbolt.org/z/xqdj4on3E – 463035818_is_not_an_ai Sep 26 '22 at 13:45
  • A good practice is to create separate target which will handle only compilation flags and all other targets will link to. I [recommend watch this](https://youtu.be/YbgH7yat-Jo) where this topic (and others) is covered. – Marek R Sep 26 '22 at 13:48
  • There is an assimp-cmake option to disable "Handle warnings as errors", check this link to learn more about it: https://kim-kulling.medium.com/assimp-hacks-1-disable-all-compiler-warnings-9dc82b3995c1 – KimKulling Oct 07 '22 at 06:41

2 Answers2

2

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})

rturrado
  • 7,699
  • 6
  • 42
  • 62
0

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.

cs1349459
  • 911
  • 9
  • 27
  • 2
    Setting a global variable for flags is considered a bad practice in modern CMake. It is recommended to apply flags on target. – Marek R Sep 26 '22 at 14:38
  • https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1#file-effective_modern_cmake-md "Get your hands off CMAKE_CXX_FLAGS." – OznOg Sep 27 '22 at 17:12