简体   繁体   中英

How do I split up my project into CMake modules that can find eachother?

I have a project with components (eg Dependency) that are very modular,

Project
-Dependency
--Include
--Src
--CMakeLists.txt
-Main
--main.cpp
--CMakeLists.txt
CMakeLists.txt

Right now, I use target_link_libraries() and include_directories() in my main CMakeLists.txt with explicit relative paths to use them.

I want to turn my dependencies into packages/modules/components/libraries/ some-things that are self-contained, so I can remove the explicit paths.

I don't want to install them. They will remain in the build tree. I want to, eg move the entire Dependency folder without breaking anything, or copy the folder into another project and have it work with just a find_package() call.

--

I have tried

target_include_directories(Dependency PUBLIC
    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>)
target_link_libraries(Dependency PUBLIC *Sub-dependencies*)

and

export(TARGETS Dependency FILE "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/DependencyConfig.cmake")

in the Dependency CMakeLists.txt, based on:

https://rix0r.nl/blog/2015/08/13/cmake-guide/ & https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/

But find_package() still fails.

--

How do I export an interface so that CMake can automatically configure dependencies between modules in the same build tree?

If you have a target_include_directories , then the dependent targets for that target can have the includes as well, depending on the PUBLIC/PRIVATE setting.

Same as target_link_directories which propagates the libraries to dependent targets.

You never need the path to the libraries, as CMake should take care of that for you. If it doesn't, it's probably a bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM