Compiling Files

Now you should have installed a compiler and have a C++ file ready for compilation. First, open a command terminal and go to the folder where your source file is located. In Windows, you should be able to press shift + right-click in the correct folder and click “Open Linux Shell here”. In macOS, the same kind of shortcut can be enabled in the Keyboard system preferences. In Ubuntu, a single right-click in the right folder should give you the option to open a terminal in the folder. To check that you are in the right folder you can type the command ls (always followed by pressing enter). A list of files should appear in the terminal – verify that your source file appears!

Compilation is then done with the command

g++ -Wall -std=c++14 -fsanitize=undefined,address filename.cpp

Since this is such a long command, people often create a shorter alias for it. In Windows and Ubuntu, this can be done through opening the alias configuration file in Visual Studio Code using the command

code ~/.bash_aliases

(in macOS the correct file is ~/.zshrc instead) and adding the line

alias c='g++ -Wall -std=c++14 -fsanitize=undefined,address'

to it. You then have to restart your terminal before it works. The alias can then be used to compile with the much shorter command

c filename.cpp

When you compile a file, you may get a series of errors and warnings. If you do not understand whan error means, they are usually easy to search for online.

When a file is compiled, an executable file called a.out is created. This is your program, and it can be run with the command

./a.out

Sometimes your program crashes. In this case, it may output a line on where it crashed (but other times, you may have to find the issue on your own).