如何在CMake Superbuild中查找dll/so文件 [英] How to find dll/so files in a CMake Superbuild

查看:101
本文介绍了如何在CMake Superbuild中查找dll/so文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用CMake Superbuild设置了跨平台软件项目,其中使用 ExternalProject_Add 添加和构建了依赖项.该项目可以在Windows和Linux上编译,但是当我尝试运行生成的可执行程序时,它无法工作,因为它无法找到依赖项的dll/so文件.这并不奇怪,因为外部依赖项的dll/so文件未放置在输出目录中(下面的可视化视图中的 bin ).

I have set up cross-platform software project using CMake Superbuild, where dependencies are added and built using ExternalProject_Add. The project compiles on both Windows and Linux, but when I try to run the executable programs that are produced, it does not work because it fails to find dll/so files of dependencies. This is not surprising, as the dll/so files of external dependencies are not placed in the output directory(bin in visualization below).

使用 ExternalProject_Add 添加的依赖项之一是OpenCV.在超级构建器构建了OpenCv之后,我可以使用 find_package(OpenCv)找到它.然后,我可以使用变量 OpenCV_LIBS OpenCV_INCLUDE_DIRS 来引用库并包含文件.但是,似乎没有变量可以告诉我dll/so文件的位置.

One of the dependencies that are added using ExternalProject_Add is OpenCV. After OpenCv is built by the superbuild, I can find it using find_package(OpenCv). I can then reference libraries and include files by using the variables OpenCV_LIBS and OpenCV_INCLUDE_DIRS. However, there seems to be no variables that tell me where the dll/so files are located.

更复杂的是,在Linux和Windows上构建时,似乎dll/so文件最终位于不同的文件夹中.这是我的项目结构的简化可视化. Build/bin 是我的项目生成的可执行程序和dll/so文件所在的位置.

To further complicate it, it seems that the dll/so files end up in different folders when building on Linux and Windows. Here is simplified visualization of my project structure. Build/bin is where my executable programs and dll/so files that my project produces end up.

<代码>|-Project1||-Source1.cpp|-Project2||-Source2.cpp|-CMake||-Superbuild.cmake|-CMakeLists.txt|-建立||-外部项目||-Bin

在Windows上,OpenCV dll最终位于 external_projects 文件夹中: external_projects/OpenCV-build/bin/Debug ,在Linux上,.so文件最终位于 external_projects/OpenCV-build/lib .

On Windows, the OpenCV dlls end up in the external_projects folder: external_projects/OpenCV-build/bin/Debug, wheras on Linux the .so files end up in external_projects/OpenCV-build/lib.

我想我可以为每个外部项目检查dll/so文件的最终存储位置,并通过使用GLOB和 if(WIN32)等组合来复制它们.但这似乎没有理想的.

I guess I could for each external project check where the dll/so files end up, and copy them by using a combination of GLOB and if(WIN32) etc. But this doesn't seem ideal.

在CMake超级构建中执行此操作的正确方法是什么,如何使dll/so文件可用,以便可执行程序可以找到它们?

What is the proper way of doing this in a CMake superbuild, how do you make the dll/so files available so the executable programs can find them?

更新需要说明的是:CMake文件夹中有一个CMakeLists.txt文件.该脚本调用Superbuild.cmake,它(经过某种程度的简化)如下所示:

Update To clarify: There is a CMakeLists.txt file inside the CMake folder. This script calls Superbuild.cmake which (somewhat simplified) looks like this:

<代码>ExternalProject_Add(OpenCV ...)#从github下载源ExternalProject_Add(libtiff ...)#从gitlab下载源...ExternalProject_Add(myMainProjectSOURCE_DIR $ {CMAKE_CURRENT_SOURCE_DIR}/..#在根项目文件夹中添加CMakeLists.txt依赖$ {myMainProjectDependencies}#OpenCV和libtiff)这将导致在构建myMainProject之前先构建依赖项.

ExternalProject_Add(OpenCV ...) # Downloads source from github ExternalProject_Add(libtiff...)# Downloads source from gitlab ... ExternalProject_Add(myMainProject SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/.. # Adds CMakeLists.txt in root project folder DEPENDS ${myMainProjectDependencies} # OpenCV and libtiff ) This results in the dependencies being built before myMainProject is built.

推荐答案

您可以使用CMake设置安装目标,并将其配置为将二进制文件和头文件放置在正确的位置:

You can setup an install target with CMake, and configure it to place your binaries and headers in the correct location:

# bin
if (${BUILD_SHARED_LIBS})
    install(
        FILES
            $<TARGET_FILE:my_target>
        DESTINATION
            bin
        COMPONENT
            runtime
    )
endif()

# lib
install(
    TARGETS
        my_target
    DESTINATION
        lib
    COMPONENT
        devel
)

# include
install(
    FILES
        Header1.hpp
        Header2.hpp
    DESTINATION
        include/my_target
    COMPONENT
        devel
)

现在,您可以使用 ninja install 之类的东西将二进制文件,导入库和标头安装到您的 CMAKE_INSTALL_PREFIX 路径(假设您使用Ninja作为生成器)CMake).

Now you can install the binaries, import libraries, and headers to your CMAKE_INSTALL_PREFIX path, using something like ninja install (assuming you're using Ninja as the generator for CMake).

这篇关于如何在CMake Superbuild中查找dll/so文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆