【问题标题】:Configure_file one time it creates a directory and its subdirectories and one time it doesn'tConfigure_file 一次创建目录及其子目录,一次不创建
【发布时间】:2019-04-12 14:01:26
【问题描述】:

我正在尝试使用configure_file 将文件从一个目的地复制到另一个目的地。我找到了以下解决方案How to copy directory from source tree to binary tree?

function(USR_copy_directory srcDir destDir)
    make_directory(${destDir})
    file(GLOB_RECURSE files RELATIVE ${srcDir} ${srcDir}/*)
    foreach(file ${files})
        set(srcFile ${srcDir}/${file})
        if(NOT IS_DIRECTORY ${srcFile})
            configure_file(${srcFile} ${destDir}/${file} COPYONLY)
        endif(NOT IS_DIRECTORY ${srcFile})
    endforeach(file)
endfunction()

这个解决方案让我能够完成这项工作。但是当我试图将for 放在另一个函数中时,它停止创建目录。它只是简单地复制了文件而不保留结构。基本上两个sn-ps的代码都是一样的,只是我删除了for循环并将它放在另一个函数中,仅此而已。我究竟做错了什么?

function(USR_copy_directory srcDir destDir)
    make_directory(${destDir})
    file(GLOB_RECURSE files RELATIVE ${srcDir} ${srcDir}/*)
    set(srcFile "")
    foreach(file ${files}) #this for loop allows me to append file and path
        list(APPEND srcFile "${srcDir}/${file}")
    endforeach(file)
    USR_copy_files("${srcFile}" ${destDir})
endfunction()


function(USR_copy_files files destDir)
    foreach(file ${files})
        if(NOT IS_DIRECTORY ${file})
            get_filename_component(filename ${file} NAME)
            configure_file(${file}  ${destDir}/${filename} COPYONLY)
        endif(NOT IS_DIRECTORY ${file})
    endforeach(file)
endfunction()

【问题讨论】:

    标签: c++ cmake


    【解决方案1】:

    它递归地复制文件(带有从 srcDir 到 destDir 的子目录,它基于 How to copy directory from source tree to binary tree? 答案

    # Copies files from source directory to destination directory, substituting any
    # variables.  Create destination directory if it does not exist.
    
    macro (configure_files srcDir destDir)
        message (STATUS "Configuring directory ${destDir}")
        make_directory (${destDir})
    
        file (GLOB templateFiles RELATIVE ${srcDir} "${srcDir}/*")
        foreach (templateFile ${templateFiles})
            set(srcTemplatePath ${srcDir}/${templateFile})
            if (NOT IS_DIRECTORY "${srcTemplatePath}")
                message(STATUS "Configuring file ${templateFile}")
                configure_file(
                        "${srcTemplatePath}"
                        "${destDir}/${templateFile}"
                        @ONLY)
            else (NOT IS_DIRECTORY "${srcTemplatePath}")
                configure_files("${srcTemplatePath}" "${destDir}/${templateFile}")
            endif (NOT IS_DIRECTORY "${srcTemplatePath}")
        endforeach (templateFile ${templateFiles})
    endmacro (configure_files srcDir destDir)
    

    【讨论】:

    • 什么?抱歉,我不明白你刚刚复制了我的问题?
    • 不,当然。看下面的 if 语句: if (NOT IS_DIRECTORY "${srcTemplatePath}") <...> else (NOT IS_DIRECTORY "${srcTemplatePath}")<...>endif (NOT IS_DIRECTORY "${srcTemplatePath}")它允许我们在保留树结构的情况下递归处理目录树
    • P.S.使用第三个参数掩码很容易扩展此方法并将“${srcDir}/*”替换为“${srcDir}/${mask}”以通过指定掩码过滤文件
    • 我知道你的代码有什么问题。您不会以递归方式调用 USR_copy_directory。因此,您不处理子目录,而只处理文件。在您的代码中修复此问题或使用我的代码
    猜你喜欢
    • 2018-12-05
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 2020-11-26
    • 2022-07-27
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多