Reputation: 361
When I want to compile, I need to specify -std=c++11
like this:
g++ -Wall -std=c++11 main.cpp -o main
and I wonder if there was a solution to set the -std=c++11 flag permanently so it will be possible to do:
g++ -Wall main.cpp -o main
without flags.
Upvotes: 14
Views: 13162
Reputation: 141
Well, here's another thing that was happening for me– I used alias g++='g++ -std=c++20'
But this worked only for the ongoing terminal session. If I were to start up another one, it would not work.
The workaround to this I found was to go to ~/.zshrc
.
The original zshrc file was:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/arnavmangla/opt/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/arnavmangla/opt/anaconda3/etc/profile.d/conda.sh" ]; then
. "/Users/arnavmangla/opt/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/Users/arnavmangla/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
added the alias command here and the new zshrc is:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/arnavmangla/opt/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
alias g++='g++ -std=c++20'
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/arnavmangla/opt/anaconda3/etc/profile.d/conda.sh" ]; then
. "/Users/arnavmangla/opt/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/Users/arnavmangla/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
Now whenever I open new sessions of terminal, the version being used in C++20
PS: This is some code I found to check the C++ Version:
#include <iostream>
using namespace std;
int main() {
if (__cplusplus == 202002L) std::cout << "C++20\n";
else if (__cplusplus == 201703L) std::cout << "C++17\n";
else if (__cplusplus == 201402L) std::cout << "C++14\n";
else if (__cplusplus == 201103L) std::cout << "C++11\n";
else if (__cplusplus == 199711L) std::cout << "C++98\n";
else std::cout << "pre-standard C++\n";
}
Upvotes: 1
Reputation: 18751
I know this already has an accepted but I feel like I have some advice to offer. For one you should be using a makefile for c++, this is the one I use for answering on SO.
CFLAGS=-std=c++11
CFLAGS+=-stdlib=libc++
CC=clang++
#flags for test.c
cc=clang
DEBUG=-g
#warnings
WARNINGS=-Weverything
#always have -Weverything on for SO lol
OPT= -O0 -O1 -O2 -O3 -O4
test: test.cpp
$(info set CC for compiler)
$(CC) $(CFLAGS) $< -o $@ $(DEBUG)
stack: stack.cpp
$(CC) $(CFLAGS) stack.cpp -o $@ $(DEBUG) $(WARNINGS)
testc: test.c
$(cc) $< -o $@ $(DEBUG)
clean:
rm test
Now whenever I download someones crappy code from SO I have a makefile for c and c++ files where I can easily change the flags if I want to.
As for bash alias I would suggest you alias it like so alias clang++11='clang++ -std=c++11
this way you don't overwrite the clang++
if you don't want to use the c++11 standard. Lastly you can add the line I just showed you to your .bash_profile
on a mac which is in your home or ~
folder, this will make the change permanent. Once you change it run source .bash_profile
to put the changes into effect. On linux I think the file is called .bashrc
. Hopefully these tips will help you out when ur c++ing, I would advise you to learn the mac command line, has differences from the linux one, it can be very useful to know some of the things it can do.
Upvotes: 5
Reputation:
Create an alias: alias g++='g++ -std=c++11'
should do the trick.
(However, the version of GCC that comes with OS X is so ancient that it doesn't support C++11, you'd be better off using clang
and clang++
.)
Upvotes: 15