How do I compile and run a simple C++ program?

Compile and run a C++ program

On Windows

If using Visual Studio on Windows, a simple F5 keypress will compile, build, and execute our program/project.

The following video explains how to install Visual Studio and compile and run a simple C++ "Hello World" program on Windows.

On Linux

On Linux, we can use the GCC (g++) compiler to compile and run our simple source.cpp program using the following command:

g++ -Wall -pedantic -std=c++14 source.cpp && ./a.out

This command compiles and links our C++ program, and if the compilation is successful, runs our newly produced a.out executable file.

If we had multiple source files in our program, then we would compile all of them using the following command-line compilation string:

g++ -Wall -pedantic -std=c++14 source.cpp source2.cpp source3.cpp && ./a.out

The following video explains how to install a C++ compiler on Linux and how to compile and run a simple C++ "Hello World" program.

On macOS

On macOS, we can use the Clang (clang++) compiler to compile and run our simple source.cpp program using the following command:

clang++ -Wall -pedantic -std=c++14 source.cpp && ./a.out

In the following video, we explain how to install a C++ compiler and compile and run a simple C++ "Hello World" program on macOS.

As you can see, when learning C++, it is fairly simple to compile and run our C++ program without the need for a complex build system.

Do I have to learn about the build systems before learning C++?

You do not have to learn CMake, Make, or any other build system to learn the C++ language.

When learning C++, we will spend most of our time with a single C++ source file, and there is no need to learn the build system. Even when we learn about the use of multiple header and source files in C++, we do not have to know about the build systems in the beginning.

That being said, we should be aware of the existence of compilation and linking processes.

What are build systems?

Build systems are software tools that automate compiling, linking, and building our software. When our C++ framework becomes fairly complex and has multiple source and header files, resulting in multiple modules, libraries, and executable files, we might want to learn the intricacies of a particular build system.

When to learn about the build systems for C++?

After learning the language and the standard library basics, you can learn about the build systems. If your C++ framework becomes more complex and includes multiple source and header files, then you might want to learn about the build systems.

What are the most popular build systems for C++?

The most popular build systems for C++ are:

  • MSBuild as part of the Visual Studio IDE on Windows
  • CMake
  • Make
  • Other build systems

In summary, we do not have to learn about the build systems in order to learn the C++ language.

Posted on May 21, 2025, by

Slobodan Dmitrovic

Slobodan Dmitrovic is a software development consultant, C++ trainer, and author of several books on C and C++.