CMake GLOB_RECURSE无法正常工作

时间:2019-06-17 14:01:27

标签: c++ cmake glob

我具有以下文件夹结构

mainfolder/
          folderA/a.dll
          folderB/subFolderB/subsubFolderB/b.dll
          folderC/c.dll

我想获取主文件夹中的所有DLL,包括子文件夹。

在主文件夹上执行GLOB_RECURSE没有任何结果。在代码到达GLOB_RECURSE调用之前,肯定存在DLL。 这是我在做什么:

set(THIRDPARTY_INSTALLFOLDER ${CMAKE_BINARY_DIR}/3rdparty/${CMAKE_BUILD_TYPE})
file(GLOB_RECURSE THEDLLS ${THIRDPARTY_INSTALLFOLDER} "*.dll")
message(STATUS ${THEDLLS})

所以${THIRDPARTY_INSTALLFOLDER}是我上面提到的主文件夹。带有${THEDLLS}的消息的输出什么也没有。

注意::再次清理/重建/运行cmake后,它会找到文件夹A中的DLL,但仅此而已(文件夹B或文件夹C中没有)。同样,这些DLL肯定是在GLOB_RECURSE调用之前创建的,而不是在CMakeLists.txt调用之前创建的。

编辑:这是一个有效的示例。 请注意,它会下载相当大的库OpenCV 。我之所以使用它,是因为我主要担心的是,DLL是根据构建它的系统而在不同的文件夹结构中创建的,这就是为什么复制DLL对我而言似乎如此困难的原因。要运行此示例,只需创建具有以下内容的CMakeLists.txt和main.cpp:

  

CMakeLists.txt

cmake_minimum_required(VERSION 3.13)
project(DownloadAndLinkOpenCV)
###First I define where to output .exe and DLLs if there were any
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build/${CMAKE_BUILD_TYPE}/bin)
###This defines the INSTALL dir of OpenCV(Where to put DLLs etc.)
set(THIRDPARTY_INSTALLFOLDER ${CMAKE_BINARY_DIR}/3rdparty/${CMAKE_BUILD_TYPE})

####function to download and build external project from https://stackoverflow.com/questions/17446981/cmake-externalproject-add-and-findpackage
function (build_external_project target git_repo git_tag cmake_custom_args)
    message(STATUS "building ${target} this might take a while...")
    set(trigger_build_dir ${CMAKE_BINARY_DIR}/3rdparty_${target})
    file(MAKE_DIRECTORY ${trigger_build_dir} ${trigger_build_dir}/build)

    set(CMAKE_LIST_CONTENT "
    cmake_minimum_required(VERSION 3.13)
    include(ExternalProject)
    ExternalProject_Add(${target}
        GIT_REPOSITORY ${git_repo}
        GIT_TAG        ${git_tag}
        PREFIX ${target}
        CMAKE_ARGS ${cmake_custom_args}
    )
ExternalProject_Add_StepTargets(${target} build install)
add_custom_target(trigger_${target})
add_dependencies(trigger_${target} ${target})
")

file(WRITE ${trigger_build_dir}/CMakeLists.txt "${CMAKE_LIST_CONTENT}")

execute_process(COMMAND ${CMAKE_COMMAND} .. -G${CMAKE_GENERATOR}
    WORKING_DIRECTORY ${trigger_build_dir}/build
    )
execute_process(COMMAND ${CMAKE_COMMAND} --build .
    WORKING_DIRECTORY ${trigger_build_dir}/build
    )
message(STATUS "finished building ${target}!")
endfunction()

####build OpenCV
set(OPENCV_GIT_REPO https://github.com/opencv/opencv.git)
set(OPENCV_TAG 3.4.6)
set(OPENCV_CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${THIRDPARTY_INSTALLFOLDER} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
build_external_project(opencv ${OPENCV_GIT_REPO} ${OPENCV_TAG} ${OPENCV_CMAKE_ARGS})
####finished building OpenCV now that it is built we can use find_package
set(OpenCV_DIR ${THIRDPARTY_INSTALLFOLDER})
find_package(OpenCV REQUIRED)

####finished 3rdparty building
add_executable(TestApplication main.cpp)
target_link_libraries(TestApplication ${OpenCV_LIBS})

####Now I want to copy the DLLs which were built but GLOB_RECURSE will not give me the name of all DLLs
####I specifically used OpenCV since the main problem is that it creates a more complex folder structure
####in the INSTALL dir. In my case it creates /x64/vc15/...the dlls (x64 is architecture and vc15 the compiler)
####I don't want to hard code the x64/vc15 string because anyone should be able to built it with any compiler
file(GLOB_RECURSE THEDLLS ${THIRDPARTY_INSTALLFOLDER} "*.dll")
message(STATUS ${THEDLLS})
  

main.cpp

#include <iostream>
#include <opencv2/core.hpp>
int main()
{
    cv::Mat Mat;
    std::cout<<"Hello World"<<std::endl;
}

请注意,我将Qt Creator用作IDE和Visual Studio 2017编译器

1 个答案:

答案 0 :(得分:0)

经过一些额外的思考,我找到了非常简单的解决方案。我给GLOB_RECURSE打错了电话。这是起作用的代码段,它可以执行我想要的操作:将文件夹内的所有DLL复制到特定目录

####get all dlls in a folder including sub folders
####The wrong call was this one: 
####file(GLOB_RECURSE THEDLLS ${THIRDPARTY_INSTALLFOLDER} "*.dll")
file(GLOB_RECURSE THEDLLS "${THIRDPARTY_INSTALLFOLDER}/*.dll")
#####This will copy the DLLs to a folder
foreach(currentDLL ${THEDLLS})
    add_custom_command(TARGET TestApplication POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy
                    ${currentDLL}
                    ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
endforeach(currentDLL)

事情是GLOB_RECURSE进行了完整的路径模式匹配。