[CMake] Dependencies of an external project

hex hex7c3 at gmail.com
Tue Nov 19 10:12:50 EST 2019


I have an external CMake project that gets exported on install. The root 
project uses it as a package. The external project must be installed 
prior being used as a package.

I can solve this problem by using `ExternalProject` for both projects 
and declare their dependency. My setup is different, though, as the root 
project depends on an external project. Since I am including the 
external project without it having installed on system I am not sure if 
it is correct to use the package as downstream or whether upstream 
should be applied instead.


Consider the following project structure:

my_test
├── cmake
├── extern
│   └── mylibrary
│       ├── cmake
│       ├── include
│       │   └── my_library
│       └── src
├── include
│   └── my_test
└── src

`mylibrary` is a standalone project with `export` on install to use its 
library as package.

`my_test` (root) is another project that depends on `mylibrary`.

CMakeLists.txt:
```
# add dependencies
add_subdirectory( extern )
add_subdirectory( src )
```

extern/CMakeLists.txt:
```
ExternalProject_Add(mylibrary
     CMAKE_ARGS
         -D CMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}
     SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mylibrary"
)
```

src/CMakeLists.txt:
```c
list( APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}" )

find_package( my_library REQUIRED )

#=============================
# Define targets
#=============================
add_executable( MyTestProject test.cpp )
add_dependencies(MyTestProject my_library-exportname)
target_link_libraries( MyTestProject PUBLIC my_library-exportname)
```

I added `add_dependencies` to have `my_library` be installed prior to 
building `my_test`. However, build fails even before that since I have 
`find_package()` in the same CMake txt file.

*How can I setup my install as dependency for a package?*

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20191119/445032fb/attachment.html>


More information about the CMake mailing list