Aller au contenu principal

Cmake Download External Project __link__ [Editor's Choice]

Introduced in CMake 3.11, FetchContent is now the preferred way to download external dependencies. Its biggest advantage is that it downloads and populates the external project during the (when you run the cmake command). Why use it?

Downloading external projects is a core task for any developer managing dependencies in C++. While git submodules were once the standard, modern CMake offers more robust, integrated ways to handle external libraries directly within your build system. cmake download external project

The ExternalProject module is the classic way to manage dependencies. Unlike FetchContent , it performs the download, configuration, and build steps during the (when you run make or ninja ). Why use it? ExternalProject — CMake 4.3.2 Documentation Introduced in CMake 3

include(FetchContent) FetchContent_Declare( googletest GIT_REPOSITORY https://github.com GIT_TAG v1.14.0 ) FetchContent_MakeAvailable(googletest) # Now you can link it directly target_link_libraries(my_test PRIVATE gtest_main) Use code with caution. 2. ExternalProject_Add: The "Superbuild" Workhorse Downloading external projects is a core task for

You can use target_link_libraries just as if the external library were a local subdirectory.

Because it runs at configuration time, the external project's targets are immediately available to your main project.

It handles nested dependencies well, ensuring a project isn't downloaded multiple times if several parts of your build depend on it. Basic Example: