CMake ExternalProject_Add() 和 FindPackage()

2023-08-28C/C++开发问题
15

本文介绍了CMake ExternalProject_Add() 和 FindPackage()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

是否有正确的方法可以找到使用 ExternalProject_Add() 构建的库(通过 FindPackage())?

Is there are proper way to find a library (via FindPackage()) which was built with ExternalProject_Add()?

问题是 CMake 在 CMake 时找不到库,因为外部库是在编译时构建的.我知道在超级构建中构建库和项目时可以组合这两个 CMake 函数,但我想在普通的 CMake 项目中使用它.

The problem is that CMake cannot find the library at CMake-time because the external library gets build at compile time. I know that it is possible to combine these two CMake function when building the library and the project in a superbuild but I want to use it in a normal CMake project.

事实上,我想使用 ExternalProject_Add 构建 VTK 6,并在我的 CMake 项目中使用 FindPackage 找到它.

In fact I would like to build VTK 6 with ExternalProject_Add and find it with FindPackage all inside my CMake project.

推荐答案

有一种方法可以做到这一点.但它有点hackish.您基本上添加了一个自定义目标,在构建期间重新运行 cmake.

there is a way to do this. but its kind of hackish. you basically add a custom target, that reruns cmake during build.

你必须在一个小型测试项目中尝试这个,以确定它是否适合你

you will have to try this in a small test project, to decide if it works for you

find_package(Beaengine)


############################################
#
#    BeaEngine
#
include(ExternalProject)
externalproject_add(BeaEngine
    SOURCE_DIR            ${PROJECT_SOURCE_DIR}/beaengine   
    SVN_REPOSITORY        http://beaengine.googlecode.com/svn/trunk/
    CMAKE_ARGS            -DoptHAS_OPTIMIZED=TRUE -DoptHAS_SYMBOLS=FALSE -DoptBUILD_64BIT=FALSE -DoptBUILD_DLL=FALSE -DoptBUILD_LITE=FALSE
    INSTALL_COMMAND       ""
 )


if(NOT ${Beaengine_FOUND})
    #rerun cmake in initial build
    #will update cmakecache/project files on first build
    #so you may have to reload project after first build
    add_custom_target(Rescan ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR} DEPENDS BeaEngine)
else()
    #Rescan becomes a dummy target after first build
    #this prevents cmake from rebuilding cache/projects on subsequent builds
    add_custom_target(Rescan)
endif()




add_executable(testapp testapp.cpp )
add_dependencies(testapp Rescan)
if(${Beaengine_FOUND})
    target_link_libraries(testapp ${Beaengine_LIBRARY})
endif()

这似乎适用于 mingw makefiles/eclipse makefile 项目.vs 会在第一次构建后请求重新加载所有项目.

this seems to work well for mingw makefiles / eclipse makefile projects. vs will request to reload all projects after first build.

这篇关于CMake ExternalProject_Add() 和 FindPackage()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6