Alright, it's time to compare CMake and Make again, but with code that uses a third-party library. Why? Because almost anything interesting or useful is going to use third-party libraries.

A quick reminder of the rules:

  1. I'll build the same code using both build systems
  2. And, I'll try to get both build systems to do roughly the same thing, within reasonable limits

Watch the video above to see how it went...

This is part of a series of videos comparing different C/C++ build systems. So click here to subscribe to the newsletter if you want to see the rest of the series.

Linking to Third-Party Libraries With CMake

One of CMake's nicest features is its FetchContent module. This can not only find third-party libraries on your system, but fetch and build missing ones. This turns what could have been a frustrating and time-consuming setup job (tracking down and manually installing all dependencies), into something that compiles out-of-the-box.

Here's the code that does this:

 

# Dependencies
include(FetchContent)

set(RAYLIB_VERSION 4.5.0)
FetchContent_Declare(
    raylib
    URL https://github.com/raysan5/raylib/archive/refs/tags/${RAYLIB_VERSION}.tar.gz
    FIND_PACKAGE_ARGS ${RAYLIB_VERSION} EXACT
)
set(BUILD_EXAMPLES OFFCACHEINTERNAL"")
FetchContent_MakeAvailable(raylib)

And then Raylib can be linked into the executable:

target_link_libraries(${PROJECT_NAME} raylib)

Linking to Third-Party Libraries With Make

Make has no FetchContent equivalent, but it's very scriptable. FetchContent can be replicated using two external tools:

  • wget - to fetch third-party libraries from the internet
  • unzip - to unarchive the downloaded source-code

With those installed, the following snippet can be added to download and build Raylib when needed:

# Build the Raylib library
$(RAYLIB_DIR)/libraylib.a:
ifeq (,$(wildcard $(RAYLIB_DIR)/Makefile))
    @echo "Downloading Raylib..."
    wget "$(RAYLIB_URL)" -O temp.zip
    unzip temp.zip -d ThirdParty/
    rm temp.zip
endif
    $(MAKE) -C $(RAYLIB_DIR)

With this done, the Makefile does roughly the same thing as the original CMakeLists.txt script. 

NOTE: There is more to the Makefile than this summary. Watch the video, or download the example code to see everything.

The CMake script is a lot shorter and simpler, and has the advantage that we can use any C/C++ toolchain. The Makefile is hardcoded for GCC. But, we have managed to do the same thing with Make, and we didn't even have to resort to running external shell scripts (although we did need two external tools: wget and unzip).

Learn How to Use CMake Properly

Want to learn how to use CMake properly? Head over to cmaketutorial.com. The CMake Tutorial goes into a lot more than I have time for here.

Build Script Examples/Templates

The build scripts for both CMake & Make are available in the Kea Campus at Creator tier or higher (link). On the Make side there's an even more advanced template that is ready for cross-compiling (in the Kea Campus).

Click here for more.