1

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.

Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
  • If gcc had a way I suspect you would find it in the [Language Standards Supported by GCC](https://gcc.gnu.org/onlinedocs/gcc/Standards.html#Standards) document. – Shafik Yaghmour Sep 09 '15 at 16:36
  • @KyleStrand I didn't mean to sound judgemental, but I simply can't really conceive of any situation in which this is necessary or even meaningful. Hence I doubt it is possible at all. – Kninnug Sep 09 '15 at 16:36

2 Answers2

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.)

rici
  • 234,347
  • 28
  • 237
  • 341
  • Interesting--I didn't realize that `make` would auto-define targets like this (i.e., I thought this would require explicitly specifying `foo` asa target). – Kyle Strand Sep 09 '15 at 17:19
  • This doesn't really address the issue of updating the standard-version as it changes, though. – Kyle Strand Sep 09 '15 at 17:33
  • 1
    @kyle: that is true. When you upgrade your compiler, you need to upgrade the makefile. (I just use one makefile and a bunch of symlinks, to simplify upgrades.) – rici Sep 09 '15 at 17:37
0

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.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • 2
    It is indeed a "moving target", but it's pretty far from what I want, I think; it appears that even in GCC 5.1, the default C++ dialect is `gnu++98`, which is...well, ancient *and* nonstandard. – Kyle Strand Sep 09 '15 at 18:05
  • @KyleStrand: I agree, it's also the only automatic moving target that I know of unfortunately (the situation is better for Clang than for gcc). – Matthieu M. Sep 10 '15 at 08:14
  • @KyleStrand Interestingly it is for C code but not for C++. Currently default standard for C is `-std=gnu11` but for C++ it's `-std=gnu++98` – user3528438 Sep 10 '15 at 13:03
  • @user3528438: It might be because C++11 introduced reused a keyword (`auto`) and introduced several others (`decltype`, `nullptr`) so the chances of conflict were greater... – Matthieu M. Sep 10 '15 at 13:09
  • I'm not so sure the situation is better for Clang, which *also* uses the C++98 standard by default--albeit ["with many C++11 features accepted as extensions"](http://clang.llvm.org/cxx_status.html). I'm not entirely sure what that even means, honestly. – Kyle Strand Sep 15 '15 at 19:00
  • 2
    Looks like GCC 6 will use GNU-14 by default. That's an improvement! I guess! – Kyle Strand Feb 23 '16 at 17:14