Problem: Projucer adds Xcode Extra Compiler Flags to both C Flags and C++ Flags, and C++20

This may be just an issue with the old version of Xcode I am using on my old develop machine here (10.3 on Mojave). I know I need to update this whole system.

But when I specify Extra Compiler Flags in the Projucer (7.0.7 develop) Xcode pane, they get added to both “Other C Flags” and “Other C++ Flags”.

This has not been a problem until now, when I am trying to figure out how to get Xcode 10.3 to build with c++20.

I set my C++ Language Standard to C++20 in Projucer. This generated an error in my project.

error: invalid value 'c++20' in '-std=c++20'
note: use 'c++98' or 'c++03' for 'ISO C++ 1998 with amendments' standard
note: use 'gnu++98' or 'gnu++03' for 'ISO C++ 1998 with amendments and GNU extensions' standard
note: use 'c++11' for 'ISO C++ 2011 with amendments' standard
note: use 'gnu++11' for 'ISO C++ 2011 with amendments and GNU extensions' standard
note: use 'c++14' for 'ISO C++ 2014 with amendments' standard
note: use 'gnu++14' for 'ISO C++ 2014 with amendments and GNU extensions' standard
note: use 'c++17' for 'ISO C++ 2017 with amendments' standard
note: use 'gnu++17' for 'ISO C++ 2017 with amendments and GNU extensions' standard
note: use 'c++2a' for 'Working draft for ISO C++ 2020' standard
note: use 'gnu++2a' for 'Working draft for ISO C++ 2020 with GNU extensions' standard
Command CompileC failed with a nonzero exit code

I then googled and found the advice to include -std=c++2a -stdlib=libc++ as Other C++ Flags.

So I add them to my Projucer settings:

Then the error is:

error: invalid argument '-std=c++2a' not allowed with 'C'
Command CompileC failed with a nonzero exit code

This seems to be because those flags are added to BOTH “Other C Flags” and “Other C++ Flags”.

So if I manually remove those flags from the “Other C Flags” setting, the project builds perfectly, with c++20.

So is there anyway to tell Projucer to only add flags to “Other C++ Flags” when it rebuilds the Xcode project?

Hi @stephenk, although the best solution here might well be that we add some way to supply separate flags for C and C++, right now you are correct that these flags will be added to both C and C++ compilers. However I think there are some alternative solutions worth considering.

For the C++ version you should be able to set the C++ Language Standard to C++20 in the Project Settings (you can access these via the cog towards the top left of the UI). However, this is only true if you want to use this version of C++ for all exporters.

For the standard library argument on Xcode libc++ is already the default argument so this can probably be omitted.

If however you do need to set this argument for any reason there is an alternative option

  • Navigate to Custom Xcode Flags in each of the configurations of your exporter (Debug and Release in your example)
  • Add CLANG_CXX_LIBRARY=libc++ (or replace libc++ with whatever you need)

Once you’ve done the above you may find your issue is resolved, if however it’s not there is still another option for Xcode exporters.

  • Navigate to Custom Xcode Flags in each of the configurations of your exporter (Debug and Release in your example)
  • Add OTHER_CPLUSPLUSFLAGS=$(OTHER_CPLUSPLUSFLAGS) -Wno-missing-braces -Wno-switch-enum -Wno-four-char-constants -std=c++2a -stdlib=libc (or whatever flags you want, each separated by a single space character)

Note the first argument, $(OTHER_CPLUSPLUSFLAGS) ensures any flags already set (for example if you’ve enabled Add Recommended Compiler Warning Flags) are not overridden by this change.

To keep this to a minimum you may like to use the Extra Compiler Flags field for any flags that work for both C and C++ and only pass the C++ arguments via this back door method.

If all else fails and you need further customisation it may be worth considering our CMake support as this should generally give you a greater level of customisation.

I hope that helps.

Thanks so much for your quick reply!

Understood, I want to eventually do this on Windows VS 2019 as well, which is my other platform at the moment.

You are correct, it’s not needed, so I’ve nixed this.

When I use this, it seems to copy the existing “Other C Flags” to “Other C++ Flags”.

Before (using the Xcode (macOS) Extra Compiler Flags field ONLY):

After, removing that last flag from Xcode (macOS), and putting it in Xcode Debug (only, for now):

  • it DOES add the extra flag at the end of the Other C++ Debug flags, but again the rest are a duplicate of the Other C flags. This seems something wrong here.

  • However, it compiles and runs perfectly fine. (!)

PS. I absolutely love the cross-platform ease of using the Projucer. I’d like to stay away from CMAKE if I can. :grin:

@anthony-nicholls - that seems like a bug in Projucer, doesn’t it? I was hoping to hear something more from you on this before I proceed with modifying the rest of my projects. I can see you’re busy from the other threads. :wink:

I’m not sure I’ll take a quick look today.

1 Like

@stephenk this seems to be an Xcode feature? It seems if you use $(OTHER_CPLUSPLUSFLAGS) as a value to OTHER_CPLUSPLUSFLAGS it will include the C flags, I also found if you specify OTHER_CFLAGS without specifying any OTHER_CPLUSPLUSFLAGS it will automatically add $(OTHER_CFLAGS) to OTHER_CPLUSPLUSFLAGS.

I think generally speaking all C flags will work as C++ flags and you probably don’t want to miss any of the C flags in your C++ flags, so I guess Xcode is trying to be helpful, therefore I think this is likely intentional behaviour.

Thanks for this! Will move forwards and see what happens.