[SOLVED] JUCE CMake and Compiler Flags

Today I finally switched one of my projects to CMake and things are compiling fine. One issue that remains, however, is that an external header I include spams my build output full with compiler warnings. Since the source is out of my control, I’m trying to disable the specific warning from that header.

I’m focusing on the Linux build before moving on to other platforms. The compiler is gcc 9.4.0. The warning I want to disable is -Wswitch-enum. The gcc flag to achieve this is -Wno-switch-enum if I’m not mistaken.

Ok, from the CMake documentation I take this is achieved by

add_compile_options(-Wno-switch-enum)

or

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-switch_enum")

However none of these seem to achieve what I am after. I also scanned the created MakeFile for the added flag and it seems it isn’t mentioned anywhere.

I’ve also tried to use juce_disable_default_flags() beforehand, no luck.

Does anyone know how to achieve what I’m afte? Any help appreciated!

I think the modern way of achiving this is using target_compile_options, e.g.

juce_add_plugin (<name> ....)
target_compile_options (<name> PRIVATE -Wno-switch-enum)

Rather than disabling the warning everywhere, it’s better to disable the warning just when including the troublesome header file, so that the warning is still active in your own code.

In JUCE, we have the following macros to turn warnings off inside a file. You could wrap your include like this:

JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wno-switch-enum")
#include "troublesome.h"
JUCE_END_IGNORE_WARNINGS_GCC_LIKE

If the include is used in several places, you might need to put the above into its own “wrapper” header file, and then update all of the #include lines to use the wrapper instead of the plain header.

If you can’t use JUCE stuff at the point where the header is included, you can use plain GCC diagnostic push pragmas, as detailed here: Diagnostic Pragmas (Using the GNU Compiler Collection (GCC))

Thanks for your answer! However, this doesn’t seem to do the trick either. CMake isn’t complaining or anything. It’s just that the option never shows up in the generated MakeFile.

Cool this solution works, thank you! However, after getting rid of thousands of lines of external warnings, I realized I had still set some -Wno-XYZ in the Projucer before. I’m not eager to fix hundreds of -Wreorder and can’t litter the entire codebase with the macro you mentioned either.

So I’m still wondering why the global compile options aren’t carried over to the MakeFile, which would be the proper solution for my situation I think.

target_compile_options is definitely the right way to go if you want the flags to apply to every file in a particular target. Do you have multiple targets in your CMake project? If so, maybe you need to add the flags to each of the targets individually.

As reuk mentioned, depending on your project structure, it might be necessary to use the PUBLIC keyword when specifying the option, e.g. target_compile_options (<name> PUBLIC -Wno-switch-enum).
This will propagate the compiler flag to all targets that link against the target you specified the flag for.

I found the problem! I generated the CMakeList with FRUTs (awesome!) Jucer2CMake tool. This linked the target juce::juce_recommended_warning_flags at the end, which was overriding the previously set compile flags.

Thanks to everyone helping out! :slight_smile:

2 Likes