C++11 on mac

Hello I would like to know if there's anything I can download on my Mac for c++11, so I would not have to type
g++ -std=c++11 -Wall -Wextra -Werror test.cpp -o test
every time I compile on terminal, and just type
g++ test.cpp -o test


I have Xcode, but I prefer to run my program through terminal.
Last edited on
Set CFLAGS somewhere, run make. This is the canonical answer.

1
2
% export CFLAGS='-std=c++11 -Wall -Wextra -Werror -pedantic-errors'
% make test # repeat this second command to rebuild 


If you have need for a more sophisticated build process, you can write a makefile.

C++11 should be default for an up-to-date GCC. You should update it as soon as you can.
How would I be able to update my gcc version? My gcc verstion is 4.2.1, but I would like 4.9 for c++11.
Gcc is C, g++ is C++. Don't be confused between these.
No matter what version gcc is, it can't handle C++.
By checking my g++ version with g++ --version, it is also 4.2.1. By reading other responses online, installing Xcode seems to be the correct way to install g++. I have the latest version of Xcode, but when I type:

g++ test.cpp -o test

If my cpp file has a constructor like:
 
Constructor() = default; 


I get warning saying that default is a 2011 extension.
Create a makefile to do stuff.
After that you just need to type "make run" and you will see wonderful result.

1
2
3
4
5
6
7
all: test
clean: test
run: test
	'./test.o'

%: %.cpp
	g++ -std=c++11 test.cpp -o './test.o'
Last edited on
I use makefiles for my projects with multiple cpp files and such, but when I only have one main cpp file I tend to use the g++ terminal command because it's easier for me to just type it up and run the program to check outputs. It's much easier to run my commands through terminal than running my code through cpp.sh because it cpp.sh crashes quite a bit, and running on my own machine compiles and builds faster than the online compiler.
closed account (48T7M4Gy)
@OP

XCode 8.2.1 which is the latest for an MBPro (and probably everything else Apple-like) comes automatically with C++11, C++14 and each is selectable via the properties of each separate project. Same goes for warnings level selections.

I have Xcode, but I prefer to run my program through terminal.

Running the program (executable) in terminal has nothing to do with it as far as I can understand from your question.

To update your Xcode just download/update it ... https://developer.apple.com/xcode/ You might have to register as a developer or use existing Apple ID which is all free including the download.
@Pattako:
You're right that gcc(1) is a C compiler, but GCC (in all caps) is the GNU Compiler Collection, which contains both gcc(1) and g++(1), the binutils, and a bunch of other optional utilities and compilers which should be updated concurrently.

@OP:
Does the CFLAGS trick work for you?

I am unsure how to update your compiler. I have no experience with Apple's computers. You may have to build it; I did this last month, but I run Linux.

I thought XCode was based of off LLVM/Clang?
How would I be able to update my gcc version? My gcc verstion is 4.2.1, but I would like 4.9 for c++11.


The current version of gcc is 6.3.1, with gcc7.0 due out soon.
closed account (48T7M4Gy)
http://stackoverflow.com/questions/39091173/how-to-enable-c17-on-mac

Not so much for the C++17 comments, but this seems to advise on updating gcc
Topic archived. No new replies allowed.