I'm currently trying to build code using gcc / g++ like this:
g++-10 -std=c++2a -Wall -Wextra -c -o hw01.o hw01.cpp
(Also the same outcome with gcc-10, gcc, g++)
And I have a static check in my Code:
static_assert(__cplusplus >= 202002L);
which sadly fails every time. As far as I've reserched, the used C++ version depends on gcc and it's version. gcc-10 has version 10.3.0 and the normal gcc command uses version 9.4.0, so both should be able to use C++20 as specified in the build command.
Yet when looking in VsCode, the variable __cplusplus evaluates to 201402L, therefore making the assertion fail.
Even when uninstalling / reinstalling the compilers (or sudo apt remove cpp) this problem persists.
Any help? How do I get my system to use a newer C++ version?
PS: I'm working on a Ubuntu WSL (host system is Windows 10)
Edit: Since most recommondations are to neglect the static test and f.e. test for a different value of __cplusplus or simply throw out the test, i'm doing this for a university assignment. The satic test is non-negotiable, can not be changed and also not altered. I have to make the test pass by changing the build value of my local C++ version, I just don't know how.
Before a future standard version is standardized, the actual value for the future version is not known. Compilers that "support" the new version before standardization will have to pick a value larger than the old one but won't have the "standard" value.
What you can do, though, is check if __cplusplus is greater than the version of the previous standard. For instance, __cplusplus in C++17 has the value 201703, so the value GCC 10.3 returns of 201709 is greater than that, so you "know" that you have a version that is newer than C++17.
The __cplusplus is meant to provide an answer to the following question: "what version of C++ does this implementation claim to support?" This is a very broad question, particular in terms of the exact definition of "support".
GCC 10.3 does not feel that its support of C++20 is sufficient to meet some definition of "support". Therefore, it does not claim to "support" C++20, even if that's the version you asked for.
This is not something you get to change. There is no compiler option to force GCC to answer the question in a different way.
If you cannot upgrade GCC to a version that is willing to claim "support" for C++20, and you cannot remove or alter the change, then you're out of luck.
Related
I have g++ installed on CentOS9
g++ (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)
I can compile with switch -std=c++20 without errors/warnings.
When I search filesystem for '11' I find
/usr/lib/gcc/x86_64-redhat-linux/11
/usr/include/c++/11
/usr/libexec/gcc/x86_64-redhat-linux/11
But when I search for '20' I get nothing.
How do I "install" C++ 20 ? What is it and how can it be done on RH/CentOS ?
11 in the file path is the compiler version, not the C++ version. So it is not a problem that there is no corresponding path with a 20.
If -std=c++20 doesn't give an error, you have (at least to some degree) C++20 support.
Theoretically there is the __cplusplus macro, which is predefined to a value of at least 202002L for enabled C++20 support, but in practice that doesn't mean that all C++20 features are supported.
There are further feature-specific feature test macros that may be of help.
For an overview of what exactly is supported in which compiler version see https://en.cppreference.com/w/cpp/compiler_support, as well as the corresponding pages of the individual compilers.
As you can see there, with GCC 11 you have mostly complete C++20 support, except for few items, especially in the standard library, such as for example full module support, constexpr for std::string and std::vector, atomic shared pointers and notably std::format.
I was searching online to see if clang supported reproducible builds. I read that GCC guaranteed reproducible builds using the -frandom-seed flag. I wanted to know if clang supports that flag and I could not find anything regarding that.I then came here which had a statement such as:
...two consecutive builds of (GCC-built) Clang
My question is what is GCC built clang ? I am currently aware of only 2 compilers (Microsoft , GCC (Coudl be Cygwin/Mingw flavor) ) and the third one was suppose to be clang. My question is what does clang (GCC built) mean ? Built from source ? I would like to think that clang is a totally different compiler from GCC and Windows. Also this documentation here states
Clang has experimental support for targeting “Cygming” (Cygwin /
MinGW) platforms.
What does that mean ? Does clang mean that it uses Mingw GCC as a compiler ? What does targeting mean here ?
To my mind, this phrase means clang was built from source using GCC as a compiler. Then, clang is a compiler, so it can't use GCC as a compiler.
Compilers are written in programming languages to be able to compile code written in a programming language. This means, a compiler can compile a compiler or even itself.
If you don't know is feature X supported in product Y, please, read the docs on product Y. If this feature isn't mentioned, it's not supported and vice versa.
I have one system that runs g++ 4.4.7 and only supports #include <cstdatomic>. I have another system that runs g++ 4.9.1 and only supports #include <atomic>.
How can I discover the earliest version of g++ that only supports <atomic>, or conversely the latest version of g++ that supports <cstdatomic>, without building all the compilers and doing a manual search?
More broadly, how can I answer this question for arbitrary system header X?
There is a gcc git mirror where I would look for these things. <cstdatomic> first appears in April 2008 with this commit and disappears in December 2009 with this one. As far as I can tell, <atomic> appears in that same commit. Looking at the tags, the latter is round about the time when gcc 4.5 was released, and sure enough, browsing through the source trees, <cstdatomic> disappears with 4.5 (but is kept in later 4.4 releases) and <atomic> appears in its place.
Addendum: The place to look in the source tree is libstdc++v3/include/. <cstdatomic> is in c_global, <atomic> in std.
More broadly, how can I answer this question for arbitrary system header X?
Wintermute shows the most reliable way, but you can also just check the release notes:
https://gcc.gnu.org/gcc-4.4/changes.html#cplusplus
https://gcc.gnu.org/gcc-4.5/changes.html#cplusplus
https://gcc.gnu.org/gcc-4.6/changes.html#cplusplus
https://gcc.gnu.org/gcc-4.7/changes.html#cxx
https://gcc.gnu.org/gcc-4.8/changes.html#cxx
https://gcc.gnu.org/gcc-4.9/changes.html#cxx
https://gcc.gnu.org/gcc-5/changes.html#cxx
They don't always list all changes, but it might be much quicker than spelunking around the source repository.
This question already has answers here:
How to determine the version of the C++ standard used by the compiler?
(9 answers)
Closed 6 years ago.
Recently I had faced compiling errors in a c++ code I wrote so I've been asked if I was using a C++11 compiler, but honestly I don't know how to check on my compiler version ! so any idea how to figure this out ??
Btw I'm using codeblocks as an IDE which includes the GCC compiler and GDB debugger from MinGW. also if I'm compiling my c++ code under Linux what command should I run to know my compiler version ?
That can be a tricky question. C++11 refers to a version of the
standard, not to a version of the compiler. Different compilers, and
different versions of any given compiler, will typically implement a mix
of versions of the standard, at least for recent versions. More or
less, because any implementation of C++11 will be fairly new, and thus
probably fairly buggy.
Most compilers have options to output the version; many will output it
systematically in verbose mode. For g++, try g++ --version. Recent
versions of g++ do have some support for C++11, but you have to activate
it with -std=c++0x (rather than the usual -std=c++03 or
-std=c++98). As the name (c++0x, rather than c++11) indicates, it
is not truly C++11; it is an implementation of some (most?) of the
major new features, in a preliminary version based on various working
papers, and not the final standard.
(FWIW: I don't think any compiler fully implements all of C++11, but I'd
love to be proven wrong.)
You can find out your compiler version like this:
g++ --version
That doesn't tell you if you are using c++11. To use c++11 features, you would have to call the compiler with thr -std=c++0x flag:
g++ -std=c++0x ....
Bear in mind that gcc doesn't implement 100% of c++11 yet, and how much it implements depends on the version. See here for a table of supported features.
EDIT: strictly speaking, if you are using GCC you cannot be using a fully compliant c++11 compiler due to the missing features. But versions 4.6.1 onwards cover a great deal of the standard.
If you're in linux, checking the version is easy.
> gcc --version
Will tell you the version you have. Note that GCC C++11 support is incomplete still, you can find the details here: http://gcc.gnu.org/projects/cxx0x.html
I've used a few C++11 features myself, namely initializer lists, and the nullptr constant. I'm using GCC 4.6 and it's working fine.
edit: And yes, as #jaunchopanza said, you'll need the -std=c++0x compiler flag to make it work. If you're using Code::Blocks, just right-click on your project, choose Build options..., and check the item that says Have g++ follow the coming C++0x ISO C++ language standard [-std=c++0x]
I am taking a programming class and we are required to use the gcc 4.1.2 compiler to compile our c++ projects. I will be creating my projects in xcode and can't find how to set that compiler. I went to the get info window on the project and hit the drop down under Compiler Version, however I do not have 4.1.2 on the list. It seems that this compiler is not installed on my computer. Does anybody know where I can download it and how I can set it as my system default gcc compiler for the term?
You can probably get away with using whatever version of GCC is on your Mac, and doing a final compile on the university machines as a check. In general, the user visible changes using a later version is stricter syntax checking, so you might do something on the Mac that won't pass a newer compiler, but that generally isn't too common. I haven't had to change more than a few lines on our 50k line codebase.
With the current developer tools, only gcc 4.0 and 4.2 are available. You could probably get gcc 4.1.2 via MacPorts, however, I'm not sure whether you can integrate it into XCode.
Before you try this, you should maybe first check with the class instructor whether it is really necessary to use exactly 4.1.2, or if it is OK to use 4.0 resp. 4.2 instead.