Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include two different versions of the same dependency in a CMake project?

I have ExternalProject in both versions 1.2 and 2.2 present in my system. ExternalProject is a CMake project and CMake finds the both versions without trouble when I ask for them. Command find_package(ExternalProject 1.2 EXACT) finds version 1.2 and find_package(ExternalProject 2.2 EXACT) finds version 2.2.

Versions 1 and 2 are not compatible with each other. The APIs are completely different.

I have a CMake project, MyProject, which has two targets, targetOne and targetTwo. TargetOne uses ExternalProject 1.2 and TargetTwo uses ExternalProject 2.2.

The below code does not do what I want. The same external dependency is not looked up twice. The compilation of TargetTwo fails. Does CMake support this scenario in any way? (except by renaming the ExternalProject version 2 and compiling it in a different location).

project(MyProject)

find_package(ExternalProject 1.2 EXACT)
add_executable(targetOne target_one.c)
target_link_libraries(targetOne ExternalProject::externalProject)

find_package(ExternalProject 2.2 EXACT)
add_executable(targetTwo target_two.c)
target_link_libraries(targetTwo ExternalProject::externalProject)
like image 424
Mikko Koivunalho Avatar asked Oct 10 '19 06:10

Mikko Koivunalho


People also ask

What is Cmakelist?

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.

How does CMake Find_package work?

CMake searches for a file called Find<package>. cmake in the CMAKE_MODULE_PATH followed by the CMake installation. If the file is found, it is read and processed by CMake. It is responsible for finding the package, checking the version, and producing any needed messages.

What is Project command in CMake?

The top-level CMakeLists. txt file for a project must contain a literal, direct call to the project() command; loading one through the include() command is not sufficient. If no such call exists, CMake will issue a warning and pretend there is a project(Project) at the top to enable the default languages ( C and CXX ).


1 Answers

You can't mix targets with the same names in the same CMakeLists.txt. Use different CMakeLists.txt - one for each executable target. Use add_subdirectory for this.

  • project ./CMakeLists.txt:
project(MyProject)
add_subdirectory(folder_one)
add_subdirectory(folder_two)
  • ./folder_one/CMakeLists.txt:
find_package(ExternalProject 1.2 EXACT)
add_executable(targetOne target_one.c)
target_link_libraries(targetOne ExternalProject::externalProject)
  • ./folder_two/CMakeLists.txt:
find_package(ExternalProject 2.2 EXACT)
add_executable(targetTwo target_two.c)
target_link_libraries(targetTwo ExternalProject::externalProject)

Also, for scope changing you can use the function

like image 81
mr NAE Avatar answered Nov 01 '22 11:11

mr NAE