Build and dependencies
A program with two source files and no third-party code is the smallest project that shows the difference between the two build systems. C++ has a project description that is itself a program, a configure step and a compiler driver. Go has a module file that names the module and its dependencies, and a command that builds the package in the current directory.
The C++ project
Three source files and a CMakeLists.txt. The header declares the function, one file defines it, and the build file lists both sources, sets the standard and adds the warnings.
#pragma once
#include <string>
std::string greet(const std::string& name);
#include "greet.h"
std::string greet(const std::string& name)
{
return "Hello, " + name;
}
#include "greet.h"
#include <iostream>
int main()
{
std::cout << greet("C++") << "\n";
}
cmake_minimum_required(VERSION 3.20)
project(greet CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(greet main.cpp greet.cpp)
target_compile_options(greet PRIVATE -Wall -Wextra)
CMake generates a build system in a directory of its choosing, which is why there are two commands. The first one probes the compiler and takes about a second on this machine:
cmake -S . -B build
-- The CXX compiler identification is GNU 15.2.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (1.2s)
-- Generating done (0.0s)
-- Build files have been written to: /tmp/build/cpp/build
cmake --build build
./build/greet
[ 33%] Building CXX object CMakeFiles/greet.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/greet.dir/greet.cpp.o
[100%] Linking CXX executable greet
[100%] Built target greet
Hello, C++
The binary is small because almost all of its code is in shared libraries:
ldd build/greet
/lib/ld-musl-x86_64.so.1 (0x7889bf4ef000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x7889bf236000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x7889bf20a000)
libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7889bf4ef000)
The Go project
The same two source files and a go.mod. The greeting lives in a package of its own, and main imports it by the module path plus the directory; there is no header, because the package is the unit the compiler reads and every exported name in it is visible to an importer.
module example.com/greet
go 1.26
package text
// Greet returns a greeting for name.
func Greet(name string) string {
return "Hello, " + name
}
package main
import (
"fmt"
"example.com/greet/text"
)
func main() {
fmt.Println(text.Greet("Go"))
}
go build -o greet .
./greet
Hello, Go
The build printed nothing, which is the Go convention: silence means success. The result is a static executable, and ldd cannot even read it as a dynamic program:
file greet
ldd greet
greet: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=81L9KgF6Ua2qvf4E63EX/1r7jHF-hunkm2DV2UQi2/MUweKBgC7hNPQcd95kEF/Gw0e83q300wc-6GAWrZ9, BuildID[sha1]=eef0729a36a5d8caf02be5fcd526963a5ad4ed1c, with debug_info, not stripped
/lib/ld-musl-x86_64.so.1: greet: Not a valid dynamic program
The Go binary is 2.4 MB against 33 KB for the C++ one, and it runs on a machine with no Go installation and no libstdc++. That trade, larger and self-contained against smaller and dependent, comes back on the size and startup page.
What a rebuild costs
Both projects were built five times after touching every source file, with hyperfine timing the commands:
hyperfine --warmup 2 --runs 6 \
'touch cpp/main.cpp cpp/greet.cpp && cmake --build cpp/build' \
'cd go && touch main.go text/greet.go && go build -o greet .'
Benchmark 1: touch cpp/main.cpp cpp/greet.cpp && cmake --build cpp/build
Time (mean ± σ): 3.015 s ± 0.134 s [User: 2.491 s, System: 0.436 s]
Range (min … max): 2.861 s … 3.290 s 10 runs
Benchmark 2: cd go && touch main.go text/greet.go && go build -o greet .
Time (mean ± σ): 109.1 ms ± 2.3 ms [User: 137.9 ms, System: 89.8 ms]
Range (min … max): 106.5 ms … 113.8 ms 10 runs
Summary
cd go && touch main.go text/greet.go && go build -o greet . ran
27.63 ± 1.35 times faster than touch cpp/main.cpp cpp/greet.cpp && cmake --build cpp/build
Twenty-seven times, and the reason is not that the Go compiler is faster at the same work. It does less of it: the C++ rebuild recompiles two translation units, each of which includes <iostream> and everything behind it, then links. Go compiled those same packages once, keeps the result in the build cache, and only recompiles the two files that changed.
A clean build reverses part of the picture. Removing the build directory costs 3.0 s for C++ and about 11 s for Go with an empty cache, because Go compiles the parts of its standard library that the program imports, and there is no precompiled library to link against. The second Go build after that is 0.28 s. A C++ project with two hundred files pays minutes for a clean build; a Go project pays seconds, and both pay a link at the end.
One new dependency
Adding a UUID library to the Go module is one command. It resolves the version, downloads it, and records the checksum of what it downloaded:
go get github.com/google/uuid@v1.6.0
go list -m all
cat go.mod
go: added github.com/google/uuid v1.6.0
example.com/greet
github.com/google/uuid v1.6.0
module example.com/greet
go 1.26
require github.com/google/uuid v1.6.0
go.sum grows by two lines, one hash of the module file and one of its archive, so a tampered download is refused. go mod tidy removes a dependency no file imports, and go mod vendor copies the sources into the repository for a build with no network. That is the whole dependency story of Go, and since 2018 it needs no tool outside the toolchain. The import in main.go is an ordinary one, import "github.com/google/uuid", and the program prints uuid.Nil; nothing else about the build changes.
C++ has no equivalent. A CMake project adds the same library through the system package manager, through FetchContent, or through vcpkg or Conan, and each choice has its own lock file, its own cache and its own way of breaking a build. The block below is the FetchContent version, and it was not run here: this image has no zlib or OpenSSL headers to link a library that is already installed, and the page does not pretend otherwise.
include(FetchContent) FetchContent_Declare( nlohmann_json GIT_REPOSITORY https://github.com/nlohmann/json.git GIT_TAG v3.11.3) FetchContent_MakeAvailable(nlohmann_json) target_link_libraries(greet PRIVATE nlohmann_json::nlohmann_json)
What differs and why
| Question | C++ with CMake | Go modules |
|---|---|---|
| What describes the build? | A program in CMake language, or a Makefile | go.mod, four lines at the start |
| What is a dependency? | A system package, a fetched source tree or a binary | A module with a version and a checksum in go.sum |
| What does the program link against? | Shared libstdc++, libgcc and libc | Nothing; the runtime is in the binary |
| What does a rebuild touch? | Every file whose headers changed | Every package whose source changed |
| What does a clean build cost here? | 3.0 s for two files | 11 s with an empty cache, then 0.28 s |
A large C++ project can be made to rebuild quickly with ccache, a precompiled header, modules or a distributed build, and every one of those is a piece of infrastructure to operate. A Go project gets the cache in the toolchain and nothing to operate. The price is the static binary and the toolchain that decides the whole build; a C++ project can swap a compiler, a standard library or a linker, and some projects need to.
The Go lessons are a module and go.mod, versions and minimal version selection and workspaces; the C++ lesson is make. The previous page of this group is concurrency.
The C++ project was built with g++ 15.2.0 (Alpine 15.2.0) through CMake 4.2.3, and the Go project with go 1.26.10; hyperfine 1.19.0 timed the rebuilds, all on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev