135

I use gcc 4.8.1 from http://hpc.sourceforge.net on Mac OSX Mountain Lion. I am trying to compile a C++ program which uses the to_string function in <string>. I need to use the flag -std=c++11 every time:

g++ -std=c++11 -o testcode1 code1.cpp

Is there a way to include this flag by default?

approxiblue
  • 6,982
  • 16
  • 51
  • 59
Guddu
  • 2,325
  • 2
  • 18
  • 23
  • 3
    Use a Makefile and put it in `CXXFLAGS`. –  Jun 02 '13 at 19:46
  • Personally I've gone with an automator service to compile using flags like this. Makefiles are also good, and then of course there's XCode (or any other IDE). – Dave Jun 02 '13 at 20:05
  • Dev-C++ and other IDEs have compiler options where you can set it – boctulus Apr 21 '16 at 11:13

4 Answers4

99

H2CO3 is right, you can use a makefile with the CXXFLAGS set with -std=c++11 A makefile is a simple text file with instructions about how to compile your program. Create a new file named Makefile (with a capital M). To automatically compile your code just type the make command in a terminal. You may have to install make.

Here's a simple one :

CXX=clang++
CXXFLAGS=-g -std=c++11 -Wall -pedantic
BIN=prog

SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)

all: $(OBJ)
    $(CXX) -o $(BIN) $^

%.o: %.c
    $(CXX) $@ -c $<

clean:
    rm -f *.o
    rm $(BIN)

It assumes that all the .cpp files are in the same directory as the makefile. But you can easily tweak your makefile to support a src, include and build directories.

Edit : I modified the default c++ compiler, my version of g++ isn't up-to-date. With clang++ this makefile works fine.

Silouane Gerin
  • 1,201
  • 10
  • 13
  • thanks Silouane and @H2CO3 :) i should learn to work with makefiles – Guddu Jun 02 '13 at 20:16
  • 1
    @guddu : Here's a [tutorial](http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/makefile.html) that seems a rather complete introduction. Then you might want to check [the special macros](http://www.cprogramming.com/tutorial/makefiles_continued.html) – Silouane Gerin Jun 02 '13 at 20:28
  • 2
    Worth noting that standard Makefile syntax requires tab characters, or it will give cryptic, idiotic errors. Which is why I use gmake with RECIPEPREFIX as shown in [the documentation](http://www.gnu.org/software/make/manual/make.html#Special-Variables). Tab characters are an abomination; use them never. – Parthian Shot Aug 20 '15 at 17:34
  • What `$^` does? And what `$<` does? – KcFnMi Oct 23 '22 at 14:37
27

As previously mentioned - in case of a project, Makefile or otherwise, this is a project configuration issue, where you'll likely need to specify other flags too.

But what about one-off programs, where you would normally just write g++ file.cpp && ./a.out?

Well, I would much like to have some #pragma to turn in on at source level, or maybe a default extension - say .cxx or .C11 or whatever, trigger it by default. But as of today, there is no such feature.

But, as you probably are working in a manual environment (i.e. shell), you can just have an alias in you .bashrc (or whatever):

alias g++11="g++ -std=c++0x"

or, for newer G++ (and when you want to feel "real C++11")

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

You can even alias to g++ itself, if you hate C++03 that much ;)

Tomasz Gandor
  • 8,235
  • 2
  • 60
  • 55
8

I think you could do it using a specs file.

Under MinGW you could run
gcc -dumpspecs > specs

Where it says

*cpp:
%{posix:-D_POSIX_SOURCE} %{mthreads:-D_MT}

You change it to

*cpp:
%{posix:-D_POSIX_SOURCE} %{mthreads:-D_MT} -std=c++11

And then place it in
/mingw/lib/gcc/mingw32/<version>/specs

I'm sure you could do the same without a MinGW build. Not sure where to place the specs file though.

The folder is probably either /gcc/lib/ or /gcc/.

DrowsySaturn
  • 1,587
  • 1
  • 10
  • 4
  • 1
    At this point in time, it is easier to get a build of gcc-6, where the default is C++14. – Marc Glisse Dec 30 '15 at 17:22
  • Under Linux, you can find the location by running `strace -f gcc your-sources-here 2>&1 | grep specs`. On one (older) Debian system, this yields `/usr/lib/gcc/i586-linux-gnu/4.9/specs`. Created that `specs` as described in the answer (needs sudo to move it there under Linux) and works like a charm! Thanks. – Adrian W Oct 13 '18 at 15:12
0

If you are using sublime then this code may work if you add it in build as code for building system. You can use this link for more information.

{
    "shell_cmd": "g++ \"${file}\" -std=c++1y -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "g++ \"${file}\" -std=c++1y -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
        }
    ]
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83