ModernCMake-Chinese
  • Introduction
  • Modern CMake 简介
    • 安装 CMake
    • 运行 CMake
    • Do's and Don'ts
    • CMake 中的新变化
  • 基础知识简介
    • 变量和 Cache
    • 在CMake 中编程
    • 与你的代码交互
    • 如何结构化你的工程
    • 运行其他程序
  • Adding Features
    • C++11 and Beyond
    • Small but common needs
    • Utilities
    • Useful modules
    • IDEs
    • Debugging
  • Including Projects
    • Submodule
    • DownloadProject
    • Fetch (CMake 3.11)
  • Testing
    • GoogleTest
    • Catch
  • Exporting and Installing
    • Installing
    • Exporting
    • Packaging
  • Looking for libraries
    • CUDA
    • OpenMP
    • Boost
    • MPI
    • ROOT
      • UseFile Example
      • Simple Example
      • Simple Example CMake 3.11+
      • Dictionary Example
    • Minuit2
Powered by GitBook
On this page

Was this helpful?

  1. Exporting and Installing

Exporting

There are two ways to access a project from another project. First is exporting targets and using them in the build directory. It usually is simply a by-product of setting up the third method (installing), but is useful for development and as a way to prepare to discuss the installation procedure.

First, you should make an export set, probably near the end of your main CMakeLists.txt:

export(TARGETS MyLib1 MyLib2 NAMESPACE MyLib:: FILE MyLibTargets.cmake)

This puts the targets you've listed into a file in the build directory, and optionally prefixes them with a namespace. Now, to allow CMake to find this package, export the package into the $HOME/.cmake/packages folder:

export(PACKAGE MyLib)

Now, if you find_package(MyLib), CMake can find the build folder. Look at the generated MyLibTargets.cmake file to help you understand exactly what is created; it's just a normal CMake file, with the exported targets.

Note that there's a downside: if you have imported dependencies, they will need to be imported before you find_package. That will be fixed in the next method.

PreviousInstallingNextPackaging

Last updated 6 years ago

Was this helpful?