Skip to content

C++ VS Code GCC CMake Setup

This page sets up a simple C++ project with GCC, CMake, GDB, Ninja, and VS Code. It is designed as a first working reference for compiling, building, debugging, and running a small C++ project from both VS Code and the terminal.

  • Install the compiler, build tools, debugger, CMake, and Ninja.
  • Verify each tool from the terminal.
  • Create a small C++ project folder.
  • Add CMakeLists.txt at the project root.
  • Add main.cpp under src.
  • Build from VS Code with the CMake extension.
  • Build from the terminal as a fallback workflow.

Install the standard Ubuntu C++ development tools. build-essential provides GCC, G++, make, and related compiler tools. cmake handles project configuration, gdb handles debugging, and ninja-build provides a fast build backend.

Terminal window
sudo apt update
sudo apt install -y build-essential cmake gdb ninja-build

Check that the compiler, CMake, debugger, and Ninja are available from the shell.

Terminal window
g++ --version
cmake --version
gdb --version
ninja --version

Create a clean project folder. Keep CMakeLists.txt at the project root and source files under src.

Terminal window
mkdir -p ~/Projects/cpp/hello_cpp/src
cd ~/Projects/cpp/hello_cpp

Target structure:

Terminal window
hello_cpp/
CMakeLists.txt
src/
main.cpp

CMakeLists.txt defines the project, C++ standard, executable target, source file path, and compiler warning flags.

Terminal window
nano CMakeLists.txt

Paste the following:

Terminal window
cmake_minimum_required(VERSION 3.16)
project(hello_cpp VERSION 1.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(hello_cpp src/main.cpp)
target_compile_options(hello_cpp PRIVATE -Wall -Wextra -Wpedantic)
  • CMAKE_CXX_STANDARD sets the language version.
  • CMAKE_CXX_STANDARD_REQUIRED prevents silent downgrade to an older standard.
  • CMAKE_CXX_EXTENSIONS OFF keeps the build closer to standard C++ instead of compiler-specific extensions.
  • target_compile_options adds useful warning flags for daily development.

Create the first source file under src. This confirms that the compiler, CMake target, and build path work end to end.

Terminal window
nano src/main.cpp

Paste the following:

Terminal window
#include <iostream>
int main() {
std::cout << "Hello from C++ on Ubuntu" << std::endl;
return 0;
}

For the VS Code workflow, install the Microsoft C++ extension and the CMake Tools extension. The C++ extension provides language support and debugging. CMake Tools provides configure, build, kit selection, and target actions.

Extension Purpose
C/C++ C++ IntelliSense, navigation, and debugging support
CMake Tools CMake configure, build, kit selection, and target workflow

Open the project root in VS Code. The project root is the folder that contains CMakeLists.txt.

Terminal window
cd ~/Projects/cpp/hello_cpp
code .

Use the Command Palette to configure and build the project through CMake Tools.

  • Open ~/Projects/cpp/hello_cpp in VS Code.
  • Open the Command Palette.
  • Run CMake: Select a Kit.
  • Choose a GCC kit.
  • Run CMake: Configure.
  • Run CMake: Build.
  • Run the compiled binary from the integrated terminal.
Terminal window
./build/hello_cpp

Use the terminal workflow when VS Code configuration is not needed or when checking whether the project builds outside the editor.

Terminal window
cd ~/Projects/cpp/hello_cpp
cmake -S . -B build
cmake --build build
./build/hello_cpp

Ninja can be selected explicitly as the CMake generator. This keeps builds fast and makes the selected build backend clear.

Terminal window
cd ~/Projects/cpp/hello_cpp
cmake -S . -B build -G Ninja
cmake --build build
./build/hello_cpp

Delete the build folder when CMake cache, compiler settings, or generator selection needs to be reset.

Terminal window
cd ~/Projects/cpp/hello_cpp
rm -rf build
cmake -S . -B build -G Ninja
cmake --build build
./build/hello_cpp

Use Debug build type when stepping through the program with GDB or VS Code debugger.

Terminal window
cd ~/Projects/cpp/hello_cpp
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake --build build

After building in Debug mode, open the compiled binary with GDB.

Terminal window
gdb ./build/hello_cpp

Common GDB commands for a first check:

Terminal window
break main
run
next
continue
quit

A successful build and run should print the hello message from main.cpp.

Terminal window
Hello from C++ on Ubuntu
  • Opening the src folder in VS Code instead of the project root that contains CMakeLists.txt.
  • Forgetting to run CMake: Select a Kit before configure.
  • Running ./build/hello_cpp before building the target.
  • Changing CMakeLists.txt but not reconfiguring the project.
  • Expecting CMake to find a source file that is not listed in add_executable.
  • Mixing old CMake cache files with a different generator. Remove build and configure again.
Terminal window
# verify tools
g++ --version
cmake --version
gdb --version
ninja --version
# configure
cmake -S . -B build -G Ninja
# build
cmake --build build
# run
./build/hello_cpp
# clean rebuild
rm -rf build
cmake -S . -B build -G Ninja
cmake --build build
# debug build
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake --build build
# run debugger
gdb ./build/hello_cpp