How to use C++ 11 features in the latest version of g++
28,097
You don't need to upgrade. Specify the standards version to g++
. For example, to compile an example program from cppreference.com:
$ g++ --version
g++ (Ubuntu 4.8.5-2ubuntu1~14.04.1) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ --std=c++11 -o test test.cpp
$ ./test
std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337
Related videos on Youtube
Author by
Pigna
Updated on September 18, 2022Comments
-
Pigna over 1 year
Newbie here. I just got an error running from the terminal a C++ program I wrote:
error: ‘stoi’ is not a member of ‘std’
. They told me the compiler is too old.I'm using Ubuntu 14.04.
My g++ version is 4.8.4.
How do I upgrade?
-
Pigna over 8 yearsso every time I use g++ I need to add
--std=c++11
? Can't I make C++11 the default standard? -
muru over 8 years@Pigna If you run
g++
that often, you should be writing Makefiles. -
charlestoncrabb almost 7 years@muru: If your machine pre-dates the standard then an update is required. Example: c++17/c++1z just released a few months ago, and my machine does not support the standard because its a few years old. Your answer is a solution to the OP's particular use case, not the posed question
-
muru almost 7 years@charlestoncrabb then you'd be looking for askubuntu.com/questions/466651/…. I don't really care about the posed question, because it's often the case that people think they want X when they want to solve Y.
-
mjwrazor almost 6 years@muru What if you are compiling someone else's code and can't edit the MakeFiles.
-
muru almost 6 yearsThat's why well-written Makefiles support
CC
,CXX
,CXXFLAGS
and the like.