A "diamond dependency" occurs when two of your libraries require different versions of the same third library. FetchContent handles this by using the first version declared. Therefore, always declare your most critical or specific dependencies at the top of your script. Missing Targets
Always use specific Git tags or commit hashes instead of branch names like "master" or "main." This prevents your build from breaking unexpectedly when the dependency updates. 2. Use Shallow Clones cmake download and build dependencies
include(ExternalProject) ExternalProject_Add( ext_openssl URL https://openssl.org CONFIGURE_COMMAND /config --prefix= BUILD_COMMAND make INSTALL_COMMAND make install ) Use code with caution. Best Practices for Dependency Management 1. Pin Your Versions A "diamond dependency" occurs when two of your
Before FetchContent, ExternalProject was the go-to tool. The key difference is timing: ExternalProject downloads and builds dependencies at build time, rather than configure time. When to Choose ExternalProject Missing Targets Always use specific Git tags or
Packages that only need to be downloaded/extracted without being linked to the main build. Implementation Example
It integrates dependencies directly into your IDE's project structure. It allows for cross-target optimization and easy debugging. It eliminates the need for pre-installed system libraries.
Managing dependencies is one of the most challenging aspects of C++ development. Historically, developers had to manually install libraries on their systems, leading to the infamous "it works on my machine" syndrome. Modern CMake has revolutionized this workflow by providing built-in tools to download and build dependencies automatically during the configuration process.