What are the differences between -std=c++11 and -std=gnu++11? - c++

What are the differences between -std=c++11 and -std=gnu++11 as compilation parameter for gcc and clang? Same question with c99 and gnu99? I know about C++ and C standards, it's the differences in the parameters that interest me.
I've read somewhere that it has to do with some extensions but it is not clear to me which ones and how to choose between one or the other for a new project.

As you have found out yourself, the difference between the two options is whether GNU extensions that violates/extend the C++ standard are enabled or not. The GNU C++ extensions are described here. You can also use most of the GNU C extensions (described here) in your C++ programs. It would be also useful to read about the -Wpedantic GCC option here.
Note that some extensions can still be in effect when using -std=c++11, as long as they do not contradict the standard. For instance, when using the MinGW compiler, I need the extensions for a working Boost.Lexical_Cast. But, as long as you don't use any of them, you are better off sticking to the standard without extensions for maximum portability. This might come in handy if you find yourself forced to change compiler.

Related

Is it possible to enable only specific C++ language features in gcc?

I have a c++14 project that currently targets gcc 7.2, and I am looking to backport code from a project that targets c++17. This project makes extensive use of if constexpr. gcc 7.2 supports if constexpr with the --std=c++1z flag, however it brings along all the other (at the time) experimental C++17 features.
Is there a way to enable only specific language features, in this case if constexpr, in gcc 7.2?
No it is not possible. It's all or nothing.
There is some limited level of control over language dialect in g++
https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html
If these dialects are used it can raise a warning and you can turn this warning into an error.
Another way could be to create plugins for clang-tidy or clang-query to check your C++ code base do not use any construct you don't want, but it becomes a rather large work to achieve the intended purpose.

How to get GCC version supports specific feature?

There are cases that I want to know since which version of gcc a specific compiler flag or c++ language feature is supported so that I can write compile control preprocessors in source files or in CMakeLists.txt. For example, the compiler flag -wno-missing-field-initializers is not supported in gcc 3.4.3(an ancient version I have to use), but I want to know exactly since which version does gcc support that flag. Where can I find such instructions?
You can check specific flags using CheckCXXCompilerFlag
For example,
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG(-Wno-missing-field-initializers RESULT_OF_TEST)
Although a more portable option, across compilers, is to use CMAKE_CXX_KNOWN_FEATURES
See https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html

Eclipse C++1y (-std=c++1y) vs -std=c++11

I'm working with eclipse and I can choose the language standard and/or other dialect flags. When I want to work with the latest language standard, do I need to declare as shown in the image or leave the language standard blank and just define c++11, or the other way around, just define the language standard c++1y and leave the flags empty?
According to Options Controlling C Dialect -std=c++1y is just a deprecated alias to -std=c++14, not to -std=c++11. Even though Eclipse offered a proper alternative option -std=c++0x, it is also deprecated, so you better leave language standard blank and set -std=c++11 among flags.

Is there a set of standard compiler options?

I am making a project using qmake and I want it to be easy to compile by many users. So far I was developing only for Linux and only for the gcc compiler. I would like my project to be compilable on other platforms too.
So far I passed the compiler options (which I found in the gcc documentation) to qmake like this:
QMAKE_CXXFLAGS += -std=c++14 \
-ffloat-store \
-O3 \
But then I realized that these options may not be valid on other compilers and tried to find equivalent options for other popular compilers, such as clang or Intel. To my surprise I found out that:
The optimization options -O0, -O1, -O2, -O3 are common to all three compilers.
The -std=c++11 and -std=c++14 options are common to gcc and clang
As far as I know, -ffloat-store and some other options are present only in gcc.
I wonder, is there some set of options, that is either formally or informally standard?
POSIX defines something about the c99 command (but AFAIK nothing about C++).
However, the qmake utility will usually be able to find out (or at least to expect) what is the C++ compiler and how to invoke it. Notice that it is generating a Makefile
Outside of Qt you might consider cmake or autoconf. They both generate Makefile-s.
See also this answer (on Programmers).
No.
The C++ standard doesn't cover any compiler option. This is something that can vary wildly between different implementations (or even different versions of the same implementation).
Off the top of my head, there's almost nothing in common between the major compilers.
The truth is, any non-trivial project requires some fine-tuning both in-code and the build process when you're aiming for multiple platforms. Your best bet is in my experience (and this is largely opinion-based), to craft the build process for each compiler to be as simple as possible, and fix most of the 'quirks' via pragmas in a platform/compiler specific include file that you "auto-include" everywhere.

ABI compatibility of different C/C++ language versions + GNU extensions

I'm currently using gcc 4.8.2 with no std option to compile my c/c++ code.
Now I would like to use some of the new C/C++ features that are provided by the newer language versions of c/c++.
(Un?)Fortunately gcc understands many flavors of C/C++:
c90, c99, c11, gnu90, gnu99, gnu11
c++98, c++03, c++11, gnu++98, gnu++03, gnu++11
Currently I'm asking myself:
Do I need for every c/c++ version a separate library or is it possible to use one library for multiple C/C++ versions?
Especially can I link a libary compiled with a specific c/c++ version, when I use the corresponding c/c++-version with the gnu extensions?
Clarification (based on the comments)
Please note, that I'm using just one compiler. Not two gcc's that only differ in the revision number.
I'm only asking for ABI incompatibilities between the different std-options, when using only one gcc compiler.
In general:
No you can't combine different language versions in the same program, this will cause "One Definition Rule" violations in many library headers.
You may find in limited cases that a few classes actually don't change with the language version. However this is rare, rvalue references are required by the C++11 Standard, and not available in C++03 mode at all.
As for versions with and without support for GNU extensions, you're likely to have more success, but you will still need to run each header through the preprocessor and verify that the exact same sequence of tokens is seen by the compiler using both options.
And that's completely apart from ABI changes, that could cause memory layout or name mangling to differ between compiler variants.
You can also avoid one-definition rule violations on your own public APIs by avoiding using any version and language-specific features. Essentially, this means a flat C API.