138

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?

3
  • 3
    Use a Makefile and put it in CXXFLAGS. Commented Jun 2, 2013 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). Commented Jun 2, 2013 at 20:05
  • Dev-C++ and other IDEs have compiler options where you can set it Commented Apr 21, 2016 at 11:13

4 Answers 4

104

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks Silouane and @H2CO3 :) i should learn to work with makefiles
@guddu : Here's a tutorial that seems a rather complete introduction. Then you might want to check the special macros
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. Tab characters are an abomination; use them never.
What $^ does? And what $< does?
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 ;)

Comments

9

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

2 Comments

At this point in time, it is easier to get a build of gcc-6, where the default is C++14.
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.
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}\"" } ] } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.