问题
I'd like to create shell aliases that will always use the "latest" version of the C/C++ standards supported by gcc/g++/clang/clang++ (one alias for C, one alias for C++). I realize that this could have multiple interpretations:
- The latest GNU-extended standard
- The latest released standard (e.g. C++14)
- The latest unreleased standard (e.g. C++1z)
- The latest standard fully implemented by the compiler (e.g. C++11 for GCC 4.9, C++14 for GCC 5+)
- ... possibly other options I haven't thought of
....but for the purpose of these shell aliases, I don't care that much which "moving" value is used as long as I know which of the above rules is used to determine which standard-mode is used.
Does either compiler include an argument-alias that tracks a moving "latest standard"? I don't see any mention of alias-options in the GCC docs (though my google-fu may simply be failing me); meanwhile, the Clang user manual mentions that "The supported modes for C are [explicit std modes...] and various aliases for those modes", but I don't see where the aliases themselves are documented.
回答1:
There is no such option in any of the compilers you mention.
However, it is easy enough to create a simple Makefile for a sandbox which includes appropriate settings. I use something like this:
CFLAGS = -Wall -std=c11 -D_XOPEN_SOURCE_=700
CXXFLAGS = -Wall -std=c++14 -D_XOPEN_SOURCE_=700
The default build rules accomplish the rest of my needs, so there is no need to fill in the makefile with the names and dependencies of all the source files, providing that I'm always sandboxing a single-file executable.
With that, I can just type make foo to compile either foo.c or foo.cc. (Or make foo.o if I don't want a link.) The feature test macro setting saves me having to remember to do that in my source files as well.
And to compile with the non-default compiler: make foo CXX=clang++. That's still less typing than clang++ -Wall -o foo foo.cc
(Actually the one I use is a bit more sophisticated than that. It includes a suffix rule for .S and some variables to simplify setting optimization and debugging options. But the principle is the same.)
回答2:
The most simple solution is to do nothing, or more specifically to avoid using -std=.
The compilers themselves raise their default mode as releases pass, in general once their developer are confident with their implementation of the Standard version. This is close to your criteria:
The latest standard fully implemented by the compiler (e.g. C++11 for GCC 4.9, C++14 for GCC 5+)
Although it also adds maturity.
来源:https://stackoverflow.com/questions/32484819/are-there-any-moving-target-alias-values-for-gcc-or-clang-std-implying-u