Dependency Management in C++

Better Dependency management is one of the most wished for features from the C++ ecosystem1. Actually people want a standardized package manager and build system, but I think most developers would settle for a good enough solution.

The problem that arises from having more that one solution is that c++ libraries rarely support dependency managers out of the box, leaving the burden of support with the package managers. Because of this too many - at least in my opinion - C++ developers do not use2 dependency/package managers.

I want to show a simple tutorial for a few options, to improve on the initial learning curve, and to highlight some issues. My initial impulse for this survey was writing a thorough contemplation on how to help C++ beginners setting up their first project (Hello World)

There are other articles34 doing the same, but I hope this one still adds to the discussion.

Declarative CMake

Many people over time have wished for a more clean declarative syntax. Unlike many other people I am actually not overly unhappy with (modern) CMake Syntax, and in my production usage I create reasonably nice target definitions.

However while working on the example of the Package Manager Post I was reminded about one aspect of CMake that is often complained a about, namely its verbosity that comes from the imperative nature of its (somewhat arcane) syntax.

So I nerd-sniped myself into playing code golf with the CMakeLists.txt and see if we could get a practically usable version that turned the boilerplatey original into a minimal .json file:

CMake.json
{   "c++"           : "23",

    "test provider" : "catch2",
    "dependencies"  : [{"id" : "jsoncpp", "target" : "JsonCpp::JsonCpp"}],

    "local-lib"     : { "type"          : "library",
                        "sources"       : [ "lib.hpp", "lib.cpp" ],
                        "tests"         : [ "lib.test.cpp" ],
                        "dependencies"  : [ "jsoncpp" ]
                      },
    "hello"         : { "sources"       : [ "main.cpp" ],
                        "dependencies"  : [ "local-lib" ]
                      }
}
The original CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 4.4)
project(packages)

set(CMAKE_CXX_STANDARD 23)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include(CTest)

find_package(jsoncpp REQUIRED)
find_package(catch2 REQUIRED)

add_library(local-lib lib.hpp lib.cpp) 
    target_link_libraries(  local-lib PRIVATE JsonCpp::JsonCpp)
    add_executable(         local-lib-test lib.test.cpp)
    target_link_libraries(  local-lib-test PRIVATE 
                                                Catch2::Catch2WithMain 
                                                local-lib )
    add_test(NAME           local-lib-test COMMAND local-lib-test)
    add_custom_target(      test-local-lib COMMAND local-lib-test)

add_executable(message main.cpp)
    add_custom_target(run-message COMMAND message)
    target_link_libraries(message PRIVATE local-lib)

The time to Hello world in C++

1. Are we there yet, or how complicated can it be?

Writing a Hello World program is a time honored tradition in programming language tutorials. "Time to Hello World" is sometimes taken as a (semi serious) measure on how good the onboarding developer experience of a programming language or developer focused tool or service is.

Optimizing every second out of the TtHW is probably not something that is very high on anyone's todo list, and rightly so. Whether it takes 3 seconds, 15 or 30 makes no difference. 5 minutes are probably fine, however to spend 15 minutes someone probably needs more motivation than idle tinkering.

Note

In case you are a beginner just looking for a hello world tutorial, you should only read until the next note. Until that point this article will still be readable in that way, even though that is not its purpose.

The actual goal of this article is to analyze some of c++ basic tooling in detail. I will be using variations of Hello World throughout, to keep the focus on the tooling while still illustrating minimal motivating examples.

Here is our good old "battle tested" C++ Hello World: