3. C++ Compilers – Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards

© Slobodan Dmitrović 2020
S. DmitrovićModern C++ for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-6047-0_3

3. C++ Compilers

Slobodan Dmitrović1 
Belgrade, Serbia
C++ programs are usually a collection of C++ code spread across one or multiple source files. The C++ compiler compiles these files and turns them into object files. Object files are linked together by a linker to create an executable file or a library. At the time of the writing, some of the more popular C++ compilers are:
  • The g++ frontend (as part of the GCC)

  • Visual C++ (as part of the Visual Studio IDE)

  • Clang (as part of the LLVM)

3.1 Installing C++ Compilers

The following sections explain how to install C++ compilers on Linux and Windows and how to compile and run our C++ programs.

3.1.1 On Linux

To install a C++ compiler on Linux , type the following inside the terminal:
sudo apt-get install build-essential
To compile the C++ source file source.cpp, we type:
g++ source.cpp
This command will produce an executable with the default name of a.out. To run the executable file, type:
To compile for a C++11 standard, we add the -std=c++11 flag:
g++ -std=c++11 source.cpp
To enable warnings, we add the -Wall flag:
g++ -std=c++11 -Wall source.cpp
To produce a custom executable name, we add the -o flag followed by an executable name:
g++ -std=c++11 -Wall source.cpp -o myexe

The same rules apply to the Clang compiler. Substitute g++ with clang++.

3.1.2 On Windows

On Windows , we can install a free copy of Visual Studio.

Choose Create a new project, make sure the C++ language option is selected, and choose - Empty Project – click Next and click Create. Go to the Solution Explorer panel, right-click on the project name, choose Add – New Item – C++ File (.cpp), type the name of a file (source.cpp), and click Add. Press F5 to run the program.

We can also do the following: choose Create a new project, make sure the C++ language option is selected, and choose – Console App – click Next and click Create.

If a Create a new project button is not visible, choose File – New – Project and repeat the remaining steps.