Cpp language versions with GNU compilers - itzjac/cpplearning GitHub Wiki

This section is completely optional and exclusive to Windows.

More compiler flags

I promised you to be able to use latest features of the C++ language (11, 14). There is only one step missing which is a compilation flag in the g++ command -std=c++11 or -std=c++14 respectively. Let's demonstrate the usage with an uniform initialization, I will use the -std=c++14. Keep in mind there might be features in the C++14 not present in C++11, this kind of problems can be solved using macros, a topic we will cover in next lessons.

#include <iostream>
#include <vector>
int main()
{
    std::cout<<"Uniform initialization"<<std::endl;
    std::vector<int> v = {1, 2, 3};
    return 0;
}

Removing the compilation flag, will obviously produce a compilation error like this

 main.cpp:7:34: error: in C++98 'v' must be initialized by constructor, not by '{...}'
     std::vector<int> v = {1, 2, 3};
                              ^
 main.cpp:7:34: error: could not convert '{1, 2, 3}' from '<brace-enclosed initializer list>' to 'std::vector<int>'

The error is in fact, providing extra information about the default version of the C++ standard in this compiler, C++98. For this standard, the initialization of vector couldn't be done using uniform initialization but manually inserting elements into the vector (you don't need to worry about that old syntax now). Any of the previous compiler flags used so far, can be combined with the C++ standard specification. It's all about what kind of code you will need to generate.

Congratulations! Now you can start coding with C++14 using GNU compilers in Windows.

Strict code

The earlier you can identify errors in you code the better. One tool that will help you on your way to write cleaner code is to enable warning through the compilation process. Getting use to compile your code with all warning flags enabled is a good practice, I encourage you to use it. -Wpedantic and -Wall are probably enough for the purpose of these lessons, if you need to dig deeper for more specific warnings you can find all the options in the official documentation g++ Warning-Options

A g++ command line that generates an obj with c++14 standar, platform specification, optimization and warnings on, might look like this


BATCH
g++ -c -std=c++14 -Wpedantic -Wall -m64 -O3 main.cpp -o main.obj

⚠️ **GitHub.com Fallback** ⚠️