C++ Development Tutorial 1: Compile a Simple Program

Domi Yan
5 min readApr 24, 2020

This is a series of tutorials to help you start on C++ development (in Linux/macOS) and get familiar with useful tools. It aims to help people who have basic knowledge of C++ language and want to know more about the C++ compilation/debugging/development process in general. The tutorial will mostly focus on how to use tools (compiler, debugger, linker, profiler, etc. ). I will only explain C++ language when necessary.

In the first tutorial, we introduce GCC, the C++ compiler. You will learn how to compile a simple program with it and some commonly used command-line options.

1. What is GCC?

GCC (GNU Compiler Collection) is a compiler system produced by GNU Project first released in 1987. [1] It has been growing over the years to support various front-end programming languages including C/C++/Fortran/Java, etc and a wide variety of backend ISAs (instruction set architectures). GCC is the default system compiler in Linux operating system. If you are on the latest FreeBSD or macOS, you will find the default compiler to be Clang. But don’t worry, their differences do not affect this tutorial, most of the content in this tutorial is shared between GCC and Clang. In the following every command line example, you can simply replace g++ with clang++ and expect it to work.

1.1 GCC and G++?

You probably have heard both. These names cause confusion. GCC stands for “GNU Compiler Collection”. It contains compilers for several programming languages. Most of the compilers for the language have their own name.[2] The C++ compiler is called G++. The C compiler is called GCC (yes, for historical reasons it’s the same as the umbrella collection “GCC”). When people talk about compiling one of those languages, they might use the compiler name (G++ for C++, GCC for C) or generally use GCC.

It’s normal to hear somebody using GCC to compile a C++ program. What they actually mean is using the compiler G++ under GCC umbrella.

2. Compile a Simple Program

We are going to use g++ (the executable name for G++ compiler) to compile our first simple example:

The program simply prints out “Hello world” and return. Let’s compile and execute the program.

In the first line, we pass in the input contains one source file “hello.cpp” to g++ compiler and use the “-o” option to specify the output executable file name as “hello”. In the second line, we execute “hello” and saw “Hello World” printed out. Hopefully, this is not too strange to you and remind you of some memories you have with C++.

3. Common GCC Compiler Options

GCC provides a lot of useful command line options. Let’s look at some of the most common ones:

-o : specify output file name

-Wall : enable all the most important and useful warnings (warnings with low rates of false positives, or which have an easy workaround to avoid, not every one)

-Werror : treat warning as errors

-D<macro>: use compile time macro

We already used -o to specify the output file name. I will explain and show examples of the rest.

3.1 -Wall: Enable Important and Useful warnings

Warnings are diagnostic messages that report constructions that are not inherently erroneous but that are risky or simply bad coding practices. In the following example, the program contains an unused variable in the main function.

compile the design with -Wall:

You can see the compiler generates a warning since variable ‘a’ is declared and not used.

Notice, the compiler can issue various warnings on different issues, this flag enables all of them. If the user wants the compiler to issue specific warnings instead of every type, they can explicitly use -W<warning type> like the following example:

3.2 -W<warning type>: Enable Specific Warning

Here is an example to warn specifically on uninitialized variables using -Wuninitialized.

In this example, the compiler is asked to and issued a warning only when it finds an uninitialized value as -Wuninitilized indicated.

You can imagine there are more warning types. To explore, visit Warning Options (Using the GNU Compiler Collection (GCC)).

3.3 -Werror: Treat Warning as Errors:

In the above example, even though the compiler issued a warning which we may want to avoid in source code, the compile process succeeded. We can change this behavior by letting the compiler treat warnings like error using -Werror. The compile process will terminate if it detects an error. Check the following example to compile the same code:

This time, we compile the same input program with an extra flag -Werror. The compiler treated the same problem as an error and stopped the process. This will force the programmer to fix the warning before they can continue compilation. This flag is used a lot to help improve and maintain the quality of code.

3.4 -D<macro>: Use Compile Time Macro

C++ macro can be defined in command line with -D command. This is useful when you want to statically change how your program is compiled without changing source code. The format of it can be one of the following:

-Dmacro_name or

-Dmacro_name=VALUE

The first one will set the macro_name’s value to 1. If your macro_name is SIZE_ARRAY and you want to set the value to be 5, add -DSIZE_ARRAY=5 to the compile command. This is a little confusing the first time at least for me since there is no separation between first letter D and the following macro name. Take a look at this example for its behavior.

In line 8, the program calculates the number of elements in the array (dividing the total array size in bytes by each element’s size) and printed it out. We can look at the output result and check if the macro passed in works.

In the first compilation, we didn’t define SIZE_ARRAY in the command line, lines 2–4 set SIZE_ARRAY to 4. In the second example, we use “-DSIZE_ARRAY” without specifying its value, the compiler will define it to default value 1. In the last one, we use “-DSIZE_ARRAY=10” and the compiler sets its value to 10. These values are verified by the output. As you can tell, we changed the programs generated by specifying different macro values in the command line without changing source code. This is a trick C++/C programmers like to use.

This section is just a glance at the options for compilers. Some other flags will be introduced in the following tutorials. To know more, check Option Summary (Using the GNU Compiler Collection (GCC)).

4. Summary

In this tutorial, you learned what GCC and G++ are and how to use G++ compiler to compile a simple C++ program. You also picked up some useful options like -Wall to enable all warnings, -Werror to turn warnings into errors, -D<macro> to define macro value in the command line. We will be using those flags in the following tutorials.

In the next one, we will learn how to compile multiple source files and use Makefile to orchestrate the process.

[1] GNU Compiler Collection

[2] G++ and GCC (Using the GNU Compiler Collection (GCC))

--

--