Skip to content

Latest commit

 

History

History
377 lines (285 loc) · 6.32 KB

File metadata and controls

377 lines (285 loc) · 6.32 KB

C/C++ Build System Options

C/C++ projects have numerous build system options, each with different philosophies and trade-offs.

Traditional Build Systems

Make

The classic Unix build tool using Makefiles:

CC = gcc
CFLAGS = -Wall -O2

myapp: main.o utils.o
	$(CC) $(CFLAGS) -o myapp main.o utils.o

main.o: main.c utils.h
	$(CC) $(CFLAGS) -c main.c

clean:
	rm -f *.o myapp

Pros:

  • Universal availability on Unix-like systems
  • Simple for small projects
  • Fine-grained control

Cons:

  • Manual dependency tracking
  • Platform-specific (requires different Makefiles for Windows/Unix)
  • Verbose for large projects
  • No automatic compiler detection

Autotools (Autoconf + Automake)

The GNU build system (./configure && make && make install):

# configure.ac
AC_INIT([myapp], [1.0])
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

# Makefile.am
bin_PROGRAMS = myapp
myapp_SOURCES = main.c utils.c utils.h

Pros:

  • Cross-platform compatibility
  • Automatic system feature detection
  • Standard for Unix/Linux software distribution

Cons:

  • Extremely complex
  • Steep learning curve
  • Slow configuration phase
  • Generates lots of files

Modern Meta-Build Systems

CMake

The most popular modern C/C++ build system:

cmake_minimum_required(VERSION 3.15)
project(MyApp VERSION 1.0)

set(CMAKE_CXX_STANDARD 17)

add_executable(myapp
    src/main.cpp
    src/utils.cpp
    include/utils.h
)

target_include_directories(myapp PRIVATE include)
target_link_libraries(myapp PRIVATE pthread)

# Find and link external libraries
find_package(Boost 1.70 REQUIRED)
target_link_libraries(myapp PRIVATE Boost::boost)

Usage:

mkdir build && cd build
cmake ..
cmake --build .

Pros:

  • Cross-platform (Windows, Linux, macOS)
  • Generates native build files (Makefiles, Visual Studio, Xcode)
  • Large ecosystem and community
  • Good IDE integration
  • Built-in testing support (CTest)
  • Package finding capabilities

Cons:

  • Own scripting language to learn
  • Can be verbose
  • Configuration can be complex for large projects
  • Variable quality of Find modules

Meson

A modern, fast build system with simpler syntax:

project('myapp', 'cpp',
  version : '1.0',
  default_options : ['cpp_std=c++17'])

sources = [
  'src/main.cpp',
  'src/utils.cpp'
]

executable('myapp', sources,
  include_directories : include_directories('include'),
  dependencies : dependency('boost')
)

Usage:

meson setup build
meson compile -C build

Pros:

  • Very fast (written in Python but builds are fast)
  • Clean, readable syntax
  • Great cross-compilation support
  • Modern design philosophy
  • Excellent documentation

Cons:

  • Smaller ecosystem than CMake
  • Fewer IDE integrations
  • Requires Python runtime
  • Less mature dependency management

Build System Generators

Bazel

Google's build system for large monorepos:

# BUILD file
cc_binary(
    name = "myapp",
    srcs = ["main.cpp", "utils.cpp"],
    hdrs = ["utils.h"],
    deps = [
        "@boost//:boost",
    ],
)

Pros:

  • Extremely fast incremental builds
  • Hermetic builds (reproducible)
  • Scales to massive codebases
  • Multi-language support
  • Remote build execution
  • Built-in testing

Cons:

  • Steep learning curve
  • Requires specific project structure
  • Overkill for small projects
  • Limited Windows support (improving)

Buck2

Meta's (Facebook) build system, successor to Buck:

cxx_binary(
    name = "myapp",
    srcs = ["main.cpp", "utils.cpp"],
    headers = ["utils.h"],
)

Pros:

  • Very fast builds
  • Good for monorepos
  • Remote execution support

Cons:

  • Smaller community than Bazel
  • Less mature ecosystem
  • Primarily designed for Meta's needs

Newer Alternatives

Ninja

A low-level build system focused on speed:

# build.ninja
cxx = g++
cflags = -Wall -O2

rule compile
  command = $cxx $cflags -c $in -o $out

rule link
  command = $cxx $in -o $out

build main.o: compile main.cpp
build utils.o: compile utils.cpp
build myapp: link main.o utils.o

Notes:

  • Usually used as a backend for CMake or Meson
  • Not meant to be written by hand
  • Extremely fast

Pros:

  • Fastest build execution
  • Minimal overhead
  • Simple design

Cons:

  • No high-level features
  • Typically generated by other tools

xmake

Lua-based build system:

target("myapp")
    set_kind("binary")
    add_files("src/*.cpp")
    add_includedirs("include")
    add_packages("boost")

Pros:

  • Simple Lua syntax
  • Built-in package management
  • Cross-platform
  • Fast

Cons:

  • Smaller community
  • Less mature than CMake

Premake

Lua-based build file generator:

workspace "MyApp"
   configurations { "Debug", "Release" }

project "myapp"
   kind "ConsoleApp"
   language "C++"
   files { "src/**.cpp", "include/**.h" }
   includedirs { "include" }

Pros:

  • Generates native IDE projects
  • Simple Lua syntax
  • Lightweight

Cons:

  • Smaller ecosystem
  • Less active development

build2

Modern build toolchain with package management:

# buildfile
exe{myapp}: cxx{main utils} hxx{utils}
cxx.std = 17

Pros:

  • Integrated package management
  • Modern C++ focus
  • Reproducible builds

Cons:

  • Small user base
  • Less documentation

Package Managers with Build Support

Conan

C/C++ package manager that works with any build system:

# conanfile.txt
[requires]
boost/1.78.0

[generators]
CMakeDeps
CMakeToolchain

Notes:

  • Not a build system itself
  • Works with CMake, Meson, etc.
  • Manages dependencies

vcpkg

Microsoft's C++ package manager:

vcpkg install boost

Notes:

  • Integrates with CMake
  • Cross-platform
  • Growing library collection

Specialized Tools

SCons

Python-based build tool:

# SConstruct
env = Environment()
env.Program('myapp', ['main.cpp', 'utils.cpp'])

Pros:

  • Python scripting
  • Cross-platform
  • Flexible

Cons:

  • Slower than modern alternatives
  • Less popular than it used to be

Choosing a Build System

  • Speed Hierarchy** (fastest to slowest):
  1. Ninja (with proper caching)
  2. Bazel (with distributed builds)
  3. Meson → Ninja
  4. CMake → Ninja
  5. Make
  6. Autotools
  • Ease of Learning** (easiest to hardest):
  1. Meson
  2. Premake/xmake
  3. Make (for basics)
  4. CMake
  5. Bazel
  6. Autotools