Skip to content

cmake: add aliases so exported target names are available in tree.#5206

Closed
teknoman117 wants to merge 1 commit intocurl:masterfrom
teknoman117:cmake-add-alias
Closed

cmake: add aliases so exported target names are available in tree.#5206
teknoman117 wants to merge 1 commit intocurl:masterfrom
teknoman117:cmake-add-alias

Conversation

@teknoman117
Copy link
Copy Markdown
Contributor

@teknoman117 teknoman117 commented Apr 9, 2020

One of the common patterns in CMake 3.x is the "superbuild" pattern, where the source trees of a dependencies are nested under a master project. In order for the projects to be modular (be agnostic to whether or not the dependencies are nested or not), the in-tree build process needs to generate the same targets as finding the dependencies out of tree. For example:

cmake_minimum_required(VERSION 3.14)
project(curl-example VERSION 0.0.1 LANGUAGES CXX)

option(USE_SYSTEM_CURL "Use the system cURL" OFF)

if (USE_SYSTEM_CURL)
    find_package(CURL REQUIRED)
else ()
    add_subdirectory(${PROJECT_SOURCE_DIR}/dependencies/curl)
endif()

add_executable(example ${PROJECT_SOURCE_DIR}/src/example.cpp)
target_link_libraries(example PRIVATE CURL::libcurl)

Using find_package to find cURL will create the CURL::libcurl target, but adding the source code directly only adds libcurl. Aliasing libcurl to CURL::libcurl lets you keep the target_link_libraries line the same regardless of the source of the dependency.

@bagder bagder closed this in 7fa1578 Apr 13, 2020
@bagder
Copy link
Copy Markdown
Member

bagder commented Apr 13, 2020

Thanks, both of you!

@teknoman117 teknoman117 deleted the cmake-add-alias branch April 13, 2020 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

cmake: add library and executable aliases to support superbuild pattern.

3 participants