In Visual Studio Code: "no template named 'initializer_list' in namespace 'std'"? [duplicate] - c++

I wanted to compile C++11 source code within Mac Terminal but failed. I tried g++ -std=c++11, g++ -std=c++0x, g++ -std=gnu++11 and g++ -std=gnu++0x but nothing worked. Terminal always read unrecognized command line option. However, g++ -std=gnu and things like that worked fine (of course C++11 source code could not pass).
Which option should I use to turn on C++11 support?
By the way, the command line tool I'm using is installed within Xcode, and I'm pretty sure that they are up-to-date.

As others have pointed out you should use clang++ rather than g++. Also, you should use the libc++ library instead of the default libstdc++; The included version of libstdc++ is quite old and therefore does not include C++11 library features.
clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
If you haven't installed the command line tools for Xcode you can run the compiler and other tools without doing that by using the xcrun tool.
xcrun clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
Also if there's a particular warning you want to disable you can pass additional flags to the compiler to do so. At the end of the warning messages it shows you the most specific flag that would enable the warning. To disable that warning you prepend no- to the warning name.
For example you probably don't want the c++98 compatibility warnings. At the end of those warnings it shows the flag -Wc++98-compat and to disable them you pass -Wno-c++98-compat.

XCode uses clang and clang++ when compiling, not g++ (assuming you haven't customized things). Instead, try:
$ cat t.cpp
#include <iostream>
int main()
{
int* p = nullptr;
std::cout << p << std::endl;
}
$ clang++ -std=c++11 -stdlib=libc++ t.cpp
$ ./a.out
0x0
Thanks to bames53's answer for pointing out that I had left out -stdlib=libc++.
If you want to use some GNU extensions (and also use C++11), you can use -std=gnu++11 instead of -std=c++11, which will turn on C++11 mode and also keep GNU extensions enabled.

Related

How can I set clang to default to C++ version 17? [duplicate]

I am using Atom as my IDE, my current __cplusplus = 201402 which is C++14 and my compiler is g++ (GCC) 9.2.0.
How do I upgrade to C++17 or C++20?
Everything I've searched up involves using another IDE (Microsoft Visual Studio).
You don't "upgrade" to newer C++ standards.
You can upgrade compiler to newer version supporting latest standards.
As of today, most compilers are set to C++14 by default.
To change it you need to pass additional argument during compilation.
For example, to compile hello.cpp with GCC for C++17 you need to execute
g++ -std=c++17 hello.cpp
You need to check how to pass compiler flags (or set standards) in your IDE / editor / build system.
I'm not familiar with Atom, but I've found this:
In the settings, click on Packages, then search for gpp-compiler. You should see a settings button – click on it and edit the command line options to suit your needs.
Do-it-yourself:
#include <iostream>
int main(void) {
std::cout << __cplusplus;
return 0;
}
Compile this firstly with the following command:
$ g++ -o main main.cpp && ./main
Thereafter:
g++ -o main main.cpp -std=c++17 && ./main
You'll get to know the differences. Note that if you're unable to use -std=c++20 flag, it clearly means that your compiler doesn't supports C++20 standard.

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?)
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
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

What's the point of including -std=c++0x in a G++ compile command?

I have recently started learning C++ and, since I'm on Linux, I'm compiling using G++.
Now, the tutorial I'm following says
If you happen to have a Linux or Mac environment with development
features, you should be able to compile any of the examples directly
from a terminal just by including C++11 flags in the command for the
compiler:
and tells me to compile using this command: g++ -std=c++0x MY_CODE.cpp -o MY_APP.
Now, what I'm wondering, what is the point of the std=c++0x flag? Is it required, or can I just run g++ MY_CODE.cpp -o MY_APP?
By default, GCC compiles C++-code for gnu++98, which is a fancy way of saying the C++98 standard plus lots of gnu extenstions.
You use -std=??? to say to the compiler what standard it should follow.
Don't omit -pedantic though, or it will squint on standards-conformance.
The options you could choose:
standard with gnu extensions
c++98 gnu++98
c++03 gnu++03
c++11 (c++0x) gnu++11 (gnu++0x)
c++14 (c++1y) gnu++14 (gnu++1y)
Coming up:
c++1z gnu++1z (Planned for release sometime in 2017, might even make it.)
GCC manual: https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Standards.html#Standards
Also, ask for full warnings, so add -Wall -Wextra.
There are preprocessor-defines for making the library include additional checks:
_GLIBCXX_CONCEPT_CHECKS to add additional compile-time-checks for some templates prerequisites. Beware that those checks don't actually always do what they should, and are thus deprecated.
_GLIBCXX_DEBUG. Enable the libraries debug-mode. This has considerable runtime-overhead.
_GLIBCXX_DEBUG_PEDANTIC Same as above, but checks against the standards requirements instead of only against the implementations.
You want to use the C++11 standard (and you are right to want that), but C++11 made a huge progress w.r.t. its older C++98 standard.
But old versions of GCC (i.e. GCC 4.8 or earlier) where not finalized before the standard itself (so they accepted the -std=c++0x flag). I strongly recommend (if you want C++11) to use the latest version of GCC, that is GCC 4.9. A bug fixing GCC 4.9.2 release appeared at end of october 2014. So use it please, and pass it the std=c++11 flag to tell the compiler you want C++11 conformance.
I actually suggest to pass std=c++11 -Wall -Wextra -g to get C++11, all warnings, and debug info. Once you have debugged your program (with gdb, and you'll better also use a recent version of gdb!) you might ask the compiler to optimize with -O2 (and perhaps -mtune=native if you want to optimize for your own computer)
Source for your reference:
main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Test main CPP" << endl;
return 0;
}
build.sh
rm demoASI*
echo "**cleaned !!**"
##### C++ 11 Compliance #####
# type ONE
g++ -o demoASI_1 -std=c++0x main.cpp
echo "**rebuild-main-done (C++ 11 Compilation) !**"
# type TWO
g++ -o demoASI_2 -std=c++11 main.cpp
echo "**rebuild-main-done (C++ 11 Compilation) !**"
##### C++ 11+ Compliance #####
# type THREE
g++ -o demoASI_3 -std=c++1y main.cpp
echo "**rebuild-main-done (C++ 11+ (i.e. 1y, but not C++14) Compilation) !**"
###### C++ 14 Compliance ######
# type FOUR
g++ -o demoASI_4 -std=c++14 main.cpp
if [ $? -eq 0 ]
then
echo "**rebuild-main-done (C++ 14 Compilation) !** :: SUCCESS"
else
echo "**rebuild-main-done (C++ 14 Compilation) !** :: FAILED"
fi
Now, execute the script as;
./build.sh (assuming build.sh has execution permission)
You can first check the version of your g++ compiler, as;
g++ --version
The version of g++, after 4.3, has support for the c++11.
Please see, for c++14 support info in compiler.

Configuring g++ from Code::Blocks doesn't take affect on command line

I'm trying to change the settings of g++ from the Code::Blocks IDE. I went to the Settings tab, clicked Compiler... and checked various options for the compiler to use, like
Enable all warnings (-Wall)
Have g++ follow the C++11 ISO C++ language standard (-std=c++11)
......
These are just two of many others; when I compile on the command line, here is what comes up:
g++ -o example example.cpp
# warning: initializer lists only available with -std=c++11 ...
Notice how there's no warning either - I have an unused variable in my program. It only works if I give the options manually:
g++ -Wall -std=c++11 -o example example.cpp
Do you think I might have done something wrong when setting up the compiler? Why aren't the options taking affect?
Invoking the compiler from the ide is completely independent from doing it in a command line shell. There's no reason for the setting and usage of one to have any effect on the other.

Cannot compile using -std flag with g++

I'm running Windows 7 with cygwin installed and trying to have a play around with some of the newer C++ features. I'm aware that in order to enable these features I have to pass g++ the -std=c++0x flag, however that gives me the following error:
cc1plus: error: unrecognized command line option "-std=c++0x"
The command line I'm issueing that gives rise to that error is:
g++-3 hello.cpp -std=c++0x -o hello
The reason for the g++-3 is because windows has trouble with the symbolic link. I've used g++ in the cygwin terminal and the result is the same anyway.
Any ideas?
You need to be using a version of GCC which supports C++ 2011 features.
This page has a list of compilers and which features each one supports. If I were you, I'd try to use GCC 4.7 if at all possible.