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

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.

Related

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

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.

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.

Error: Range-based 'for' loops are not allowed in C++98 mode

So I'm following the tutorials on this page:
http://www.cplusplus.com/doc/tutorial/control/
But I'm having trouble doing a range/based for loop. I found this page:
GNU GCC compiler updatingThe answer there says I should open "Project" and "Properties". But when I try that, the "Properties" option is grayed out with no explanation:
http://imageshack.com/a/img571/4371/xd1x.png
So.. how can I activate range/based for loops?
Pass -std=c++11 flag to the compiler. Certainly GCC should be fresh enough (>=4.7) to support all these modern standards. For CodeBlocks 13.12: Settings -> Compiler -> Tab "Compiler Flags" -> Option "Have g++ follow the C++11 ISO C++ [-std=c++11]"
The above given solution of using -std=c++11 didn't work for me.
This is the target and version detail of my compiler.
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
Target: x86_64-linux-gnu
When I tried, this is what happened.
$ g++ -std=c++11 program.cpp
cc1plus: error: unrecognized command line option ‘-std=c++11’
This solved the problem for me.
$ g++ -std=c++0x program.cpp
In Dev-Cpp 5.11 Simply you can click on Tools>Compiler Options>Settings>Code Generation>(and in last option)Language standard(-std) select from dropdown (ISO C++ 11).
If you faced this error in Code::Blocks, this might help you -
Click on Settings -> Compiler -> Compiler Settings -> Compiler Flags
Under the "General" section, check [✓] the box beside :
Have g++ follow the C++11 ISO C++ language standard [-std=c++11]
Both of these:
g++ -std=c++11 -o test_executable test_source.cpp
g++ -std=c++0x -o program program.cpp
worked for me.
Only thing to do after compiling is to execute the test_executable (in the first case) as ./test_executables or program (in the second case) as ./program.
Using the above solution
g++ -std=c++0x program.cpp
works. However, the command needs to be modified slightly in order to run the program with the common command:
./program
I used
g++ -std=c++0x -o program program.cpp
and all worked perfectly.
If you are using QT5.5, you can achieve it by adding following lines in your .pro file.
CONFIG += c++11
The best solution is doing this, in Dev C++:
Go to the "Tools" Option
Select "Compiler Options"
Click "Settings"
Click "Code Generation"
Go to "Choose Language" and select "ISO C++11"
Then your problem is resolved.
in first time, if you have Dev-C++
C:\Program Files (x86)\Dev-Cpp\MinGW64\bin, you must add this route to the path
then use the next command in cmd.
g++ -std=c++11 -o outprogram code_source.cpp
this command in your project directory.

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.