ytgsyk4h
ytgsyk4h

Reputation: 25

Including CMake Project Libraries

I'm in the process of putting together a small c++ project using CMake for the first time. My current project structure is

├── bch
│   ├── CMakeLists.txt
│   ├── gf
│   │   ├── CMakeLists.txt
│   │   ├── include
│   │   │   └── gf.h
│   │   └── src
│   │       └── gf.cpp
│   ├── include
│   │   └── bch.h
│   └── src
│       └── bch.cpp
├── bsc
│   ├── CMakeLists.txt
│   ├── include
│   │   └── bsc.h
│   └── src
│       └── bsc.cpp
├── CMakeLists.txt
├── .gitignore
└── main.cpp

Currently I have gf as a subdirectory of bch. The contents of bch/CMakeLists is

cmake_minimum_required(VERSION 3.17)
project(bch VERSION 0.1.0)

# Targets
add_library(bch STATIC src/bch.cpp)

# Dependant
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/gf)
target_link_libraries(bch PUBLIC gf)

# Export to dependants
target_include_directories(bch PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

I would like to take the gf CMake project and place outside of the directory path of bch. This doesn't seem to be a supported structure when using the add_subdirectory command unless I'm missing something. Generally, what would be the current "Best Practice" for accomplishing my goal of decoupling the directory structure from the dependency tree?

Upvotes: 0

Views: 180

Answers (1)

b00rt00s
b00rt00s

Reputation: 112

If you want to decouple the project from dependencies, then I would suggest splitting cmake project into two separate, exporting the dependent target and then importing it with 'find_package'. Here is quick google find for that topic: https://visualgdb.com/tutorials/linux/cmake/find_package

[edit]

For a more general approach I suggest cmake documentation:

https://cmake.org/cmake/help/latest/command/find_package.html#command:find_package

https://cmake.org/cmake/help/latest/module/CMakePackageConfigHelpers.html

https://cmake.org/cmake/help/v3.18/command/install.html#export

The idea is:

  1. In a dependency project to generate a '*ConfigVersion.cmake' file, install all needed files (headers, binaries and *ConfigVersion.cmake) using the 'EXPORT' in parameter the 'install' command.

  2. In a final project use 'find_package' to import the dependency.

For bigger library projects I also suggest using namespaces to allow importing only selected parts of the library.

Upvotes: 1

Related Questions