Abhijeet Khangarot
Abhijeet Khangarot

Reputation: 1567

Configuring C++11/14 in Mac Terminal by default

I want to use c++11/14 features like range-based loops, but get a warning while doing g++ program.cpp. If done with compiler flag g++ -std=c++11 program.cpp the warning goes away. Is there a way to use c++11/14 by default on the g++ command (i.e without passing compiler flag every time).

Please explain to someone with limited knowledge of compilers and only need the c++11/14 features for competitive programming problems (even if it's a bad idea in general, maybe due to backward compatibility?)

Upvotes: 1

Views: 1250

Answers (2)

Y.T.
Y.T.

Reputation: 2748

Short Answer: Update your g++

According to g++ documentation

The default, if no C++ language dialect options are given, is -std=gnu++17.

You are probably using an older version of g++. You can check it using by running g++ --version in your terminal. If you are using Linux, you can also extract your default c++ standard from g++ manual with the command man g++ | col -b | grep -B 1 -e '-std.* default' in your terminal.

If you do not want to update your g++, you can also set a command alias by putting something like alias g+++='g++ -std=c++14' in your .bashrc

Upvotes: 2

Tzig
Tzig

Reputation: 841

If you're using a gcc version > 4.9.3 use this command: g++ -std=c++14 program.cpp

If you're using an older version than that use g++ -std=c++1y program.cpp

Note: Consider adding the -Wall flag before program.cpp in your command to get warnings, they help you way more than you'd think!

Tip: If you're a starting developer and don't want too steep of a learning curve, try using an IDE before going full command-line.

EDIT: If you want a command to be "the default" you can add something like alias mycc='g++ -std=c++14 -Wall' in your .bashrc or .bash_profile file (see this link), then you'll be able to use mycc program.cpp

Upvotes: 3

Related Questions