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?
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):or, for newer G++ (and when you want to feel "real C++11")
You can even alias to
g++
itself, if you hate C++03 that much ;)I think you could do it using a specs file.
Under MinGW you could run
gcc -dumpspecs > specs
Where it says
You change it to
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/.
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 :
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.