Compile C++ using GCC¶
GCC (shorthand for 'GNU Compiler Collection') is a collection of compilers able to compile multiple different programming languages.
This page describes how to compile C++ code using the GCC.
Procedure¶
0. Create a C++ source file¶
You will need C++ code to work on.
In this optional step, a file with a minimal C++ program is created.
Create and write a C++ source file called hello_world.cpp
:
In nano, write the C++ program as such:
1. Load a GCC module¶
Load a recent GCC module:
Do I really need to load a module?
No, as there is a system-installed GCC.
For sake of doing reproducible research, always load a module of a specific version.
2. Compile the source file¶
After saving and closing nano, compile as such:
This compiles the file hello_world.cpp
using all defaults:
- default/no optimization
- the executable created is called
a.out
To compiles the file hello_world.cpp
with run-time speed optimization
and creating an executable with a more sensible name, use:
-O3
: optimize for run-time speed-o hello_world
: the executable created is calledhello_world
3. Run¶
Run the program:
Output: