Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

build system - How to use libraries within my CMake project that need to be installed first?

I have a problem with my CMake build system. There are CMakeLists.txt files defining runtimes or libraries or using ExternalProjects_Add() to download and build external code. Because of dependencies, those projects have to find each other. Now I want to have a CMakeLists.txt at the top level that builds all those at once. In order to find a project, is must be installed. But finding projects is already done at configuration time in CMake.

repository
├─project
│ ├─game (Depends on engine, uses EngineConfig.cmake once installed)
│ │ ├─CMakeLists.txt
│ │ ├─include
│ │ ├─src
│ │ └─textures
│ ├─engine (Depends on boost, uses built-in FindBoost.cmake)
│ │ ├─CMakeLists.txt
│ │ ├─include
│ │ └─src
│ ├─boost (Not the source code, just an ExternalProject_Add call)
│ : └─CMakeLists.txt
│
├─build
│ ├─game
│ ├─engine
│ ├─boost (Source will be downloaded and built here)
│ : ├─download
│   ├─source
│   :
│
├─install
│ ├─game
│ │ ├─bin
│ │ └─textures
│ ├─engine
│ │ ├─include
│ │ │ └─engine
│ │ │   ├─EngineConfig.cmake (Needed to find the library)
│ │ │   :
│ │ │
│ │ └─lib
│ ├─boost (Layout is up to the external library)
│ : └─ ...
│
└─CMakeLists.txt (Calls add_subdirectory for all inside the project folder)

Run a CMake process for every project: Using execute_process(${CMAKE_COMMAND} ...), I can configure and build each project after another at configure time. However, this means I always have to run CMake after editing the code and cannot compile from within the IDE I generated project files for.

Linking to CMake targets: Running a CMake process for all external libraries is okay since I don't work on them. My own libraries could be used by calling target_link_libraries() with their target names. However, linking isn't enough. My libraries include directories of external libraries. Those must be made available to the using project, as well.

How can I use libraries within my CMake project that need to be installed first?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can classify your projects into three groups:

  1. External dependencies you are not working on in this super-project
  2. Projects you're working on but are too complex to add them as a subdirectory, for example having too many targets or other reasons. (You don't seem to have such a project in your example.)
  3. Projects you're working on: these will be added as a subdirectory of the super-project.

You need to configure, build and install the project in groups #1 and #2 before configuring the super-project:

  • You can do it before running the super-project's CMakeLists.txt, for example, from a shell-script
  • Or, as you mentioned, from within the super-project's CMakeLists.txt, using execute_process(${CMAKE_COMMAND} ...). You can do it conditionally using the result of an appropriate find_package(... QUIET) command.

You need to decide if projects in group #3, like engine will be used solely in projects that uses them as subdirectories or you intend to use them as standalone libraries, built in their own build trees.

Also, you mentioned that: "My libraries include directories of external libraries". Let's cover all such possible libraries the engine can be dependent on:

  • say, LIB1 and LIB2 are private and public external dependencies of engine and their config-modules export old-school LIB1_* and LIB2_* variables
  • LIB3 and LIB4 are private and public external dependencies of engine and their config-modules export the LIB3 and LIB4 imported libraries

By public and private dependencies I mean whether the particular library is used or not used on the interface of engine.

Now, if engine is to be used only as a subdirectory then the relevant section of engine/CMakeLists.txt is:

add_library(engine ...)
target_include_directories(engine
    PRIVATE
        ${LIB1_INCLUDE_DIRS}
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        ${LIB2_INCLUDE_DIRS})
target_compiled_definitions(engine
    PRIVATE ${LIB1_DEFINITIONS}
    PUBLIC ${LIB2_DEFINITIONS})
target_link_libraries(engine
    PRIVATE LIB3
    PUBLIC LIB4)

in repository/CMakeLists.txt:

add_subdirectory(engine)
add_subdirectory(game)

in game/CMakeLists.txt:

add_executable(game ...)
target_link_libraries(game engine)

The include dirs of both of the engine and of its public dependencies will be correctly forwarded to game.

If engine will also be built in its own build tree (in another project) you need to add the exporting code to engine/CMakeLists.txt and maybe a custom config-module that calls find_package (or find_dependency) for its dependencies. See How to use CMake to find and link to a library using install-export and find_package? for details. One issue not discussed in that answer is finding the dependencies of a library in the library's config module:

The referenced SO answer simply installs the <lib>-targets.cmake script, generated by the install(EXPORT ...) command, as the config-module:

install(EXPORT engine-targets
    FILE engine-config.cmake
    DESTINATION lib/cmake/engine)

This solution is fine when engine has no further dependencies. If it does, they need to be found at the beginning of the config module, which should be written manually.

engine/engine-config.cmake:

include(CMakeFindDependencyMacro)
find_dependency(some-dep-of-engine)
include(${CMAKE_CURRENT_LIST_DIR}/engine-targets.cmake)

and in engine/CMakeLists.txt:

install(EXPORT engine-targets
    FILE engine-targets.cmake
    DESTINATION lib/cmake/engine)
install(FILES engine-config.cmake
    DESTINATION lib/cmake/engine)

Note: The CMakeFindDependencyMacro has been introduced in CMake 3.0. With an older CMake you can use find_package instead of find_dependency (handling of QUIET and REQUIRED options will not be forwarded to the dependency).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...