Operations Research

Logo

A site for hosting software and data repositories associated with papers appearing in the journal _Operations Research_

View source on GitHub

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:

Cons:

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:

Cons:

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:

Cons:

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:

Cons:

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:

Cons:

Buck2

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

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

Pros:

Cons:

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:

Pros:

Cons:

xmake

Lua-based build system:

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

Pros:

Cons:

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:

Cons:

build2

Modern build toolchain with package management:

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

Pros:

Cons:

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:

vcpkg

Microsoft’s C++ package manager:

vcpkg install boost

Notes:

Specialized Tools

SCons

Python-based build tool:

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

Pros:

Cons:

Choosing a Build System