2

I have been using Ubuntu for so long, and a few days before I decided to switch to Linux Mint. On ubuntu, I never had any problems on compiling C++ source code written for the c++14 standard. But on Mint, the default standard is c++98. I tried to make an alias as follows:

alias g++="g++ -std=c++14

and it worked for small programs that I manually compile. However, this is not a solution for automatic software building with Makefiles, so I want to tell g++ to use c++14 as the default ISO. Thanks for your help

I want this for me, more concretly I want to do something like:

std::vector<int> numbers = {1, 2, 3, 4, 5};

ifconfig
  • 6,242
  • 7
  • 41
  • 65
Sergio Quijano
  • 21
  • 1
  • 1
  • 3
  • 6
    Set the standard you want to use explicitly in your buildsystem, you don't want your build to break because some system happens to have different defaults than yours. – Baum mit Augen Jan 24 '18 at 19:40
  • 4
    In the makefile, add `-stdc++14` to the compilation flags (e.g. to `CXXFLAGS` variable). In other words, set up the makefile which orchestrates building to do the build as required, not the compiler. – Peter Jan 24 '18 at 19:42
  • 1
    The most complete remediation is likely to change GCC's [SPEC file](https://stackoverflow.com/q/5778449/608639) so `-std=c++14` is added if and `-std` option is missing. – jww Jan 24 '18 at 20:29
  • @jww You should consider to write another answer referring to that Q&A you linked, or propose this as a possible duplicate. –  Jan 24 '18 at 20:48
  • @jww not if your code is also meant to be build by other people who haven't done that... the source (including makefiles or whatever) should be written to work with default compiler installs – M.M Jan 25 '18 at 00:49
  • Thanks @M.M. That sounds like a different problem - the code does not compile without C++14. I think OP wants to use `-std=c++14` as a default for his local installation. – jww Jan 25 '18 at 01:10
  • This shows how to modify `gcc` spec file in-place: https://stackoverflow.com/a/17224826/412080 – Maxim Egorushkin Apr 09 '20 at 22:02
  • If using cmake add this to `CMakeLists.txt`:`set( CMAKE_CXX_STANDARD 14 )` – wcochran Nov 12 '22 at 18:41

2 Answers2

2

Set an environment variable in one of your .rc scripts:

export CXXFLAGS = "$CXXFLAGS -std=c++14"

that should affect all calls to make unless the variable is explicitly set in the Makefile again.


Another option is to provide a config.mak file, you can include in your Makefiles:

Makefile:

include config.mak

# ... rules and actions

config.mak:

CXXFLAGS += "-std=c++14"
  • 1
    Too many Autotools projects don't honor `CFLAGS` and `CXXFLAGS`. Neither does CMake. It is a pandemic problem. The most complete remediation is likely to change GCC's [SPEC file](https://stackoverflow.com/q/5778449/608639). – jww Jan 24 '18 at 20:27
  • @jww Good idea. Though it's still unclear if the OP want's to use autotools generated build systems, or their own. –  Jan 24 '18 at 20:29
-1

That's just not how compilers and software distribution on Linux distributions work.

There are many build systems, ranging from plan Makefiles, Autotools, scons, CMake, qmake, waf, Bazel, Buck, Mezon... you name it. Each and every one of these has a distinct method on how to specify the compiler and its options.

A compiler's default settings also affect behavior, and these defaults are different in each compiler's version.

Also, as a user, or someone who is "just" compiling software written by someone else, you should not be overriding the author's idea about a language standard in the general case. Doing that it potentially dangerous.

If, however, this is about your own software and your convenience, then my suggestion is to use a real build system which lets you specify the compiler options and the language standard globally, from one location. You are also setting other non-default flags such as -Wall, aren't you?

Jan Kundrát
  • 3,700
  • 1
  • 18
  • 29