代码之家  ›  专栏  ›  技术社区  ›  Marenz

使用GLOB指定源文件还是在CMake中单独指定每个文件更好?

  •  199
  • Marenz  · 技术社区  · 17 年前

    documentation

    FILE(GLOB MY_SRCS dir/*)
    

    另一种方法是单独指定每个文件。

    7 回复  |  直到 7 年前
        1
  •  162
  •   ideasman42    9 年前

    完全披露:我最初更喜欢globbing方法,因为它很简单,但多年来我逐渐认识到,对于大型、多开发人员项目来说,明确列出文件不太容易出错。


    全球化的优点是:

    list(REMOVE_ITEM)

    file(GLOB to_remove file_to_remove.cpp)
    list(REMOVE_ITEM list ${to_remove})
    

    这可能会伤害你的唯一真实情况是,如果你正在使用类似的东西 git-bisect

        2
  •  97
  •   ideasman42    10 年前

    .

    使用球状物。

    Filesystem

    (我们不建议使用GLOB从源代码树中收集源文件列表。如果在添加或删除源代码时没有CMakeLists.txt文件更改,则生成的构建系统无法知道何时要求CMake重新生成。)


    globbing的最大缺点是创建/删除文件不会自动更新构建系统。

    如果你是添加文件的人,这似乎是一个可以接受的权衡。但是,这会给构建你的代码的其他人带来问题;他们从版本控制更新项目,运行构建,然后联系您,抱怨


    git bisect

    CMakeLists.txt 这并不总是足够的。对于使用globbing的自动化构建,我必须运行 cmake 每一个构建 自上次建造以来已添加/删除*。

    有时,球化更可取:


    • 有用,但你必须接受跑步 生成构建文件 每次 (这违背了CMake的意图——将配置与构建分离的能力) .

    是的,我本可以编写一段代码来比较更新前后磁盘上的文件树,但这不是一个很好的解决方法,最好留给构建系统。

        3
  •  8
  •   Glen Knowles    10 年前

    file(GLOB ...) and file(GLOB_RECURSE ...) 命令获得a CONFIGURE_DEPENDS 由于这是源文件全局搜索的主要缺点,现在可以这样做了:

    # Whenever this glob's value changes, cmake will rerun and update the build with the
    # new/removed files.
    file(GLOB_RECURSE sources CONFIGURE_DEPENDS "*.cpp")
    
    add_executable(my_target ${sources})
    

    the documentation

    CONFIGURE_pends CONFIGURE_pends

    就我个人而言,我认为不必手动管理源文件列表的好处超过了可能的缺点。如果您必须切换回手动列出的文件,只需打印全局源列表并将其粘贴回去即可轻松实现。

        4
  •  -1
  •   palfi    8 年前

    # Compare the new contents with the existing file, if it exists and is the
    # same we don't want to trigger a make by changing its timestamp.
    function(update_file path content)
        set(old_content "")
        if(EXISTS "${path}")
            file(READ "${path}" old_content)
        endif()
        if(NOT old_content STREQUAL content)
            file(WRITE "${path}" "${content}")
        endif()
    endfunction(update_file)
    
    # Creates a file called CMakeDeps.cmake next to your CMakeLists.txt with
    # the list of dependencies in it - this file should be treated as part of
    # CMakeLists.txt (source controlled, etc.).
    function(update_deps_file deps)
        set(deps_file "CMakeDeps.cmake")
        # Normalize the list so it's the same on every machine
        list(REMOVE_DUPLICATES deps)
        foreach(dep IN LISTS deps)
            file(RELATIVE_PATH rel_dep ${CMAKE_CURRENT_SOURCE_DIR} ${dep})
            list(APPEND rel_deps ${rel_dep})
        endforeach(dep)
        list(SORT rel_deps)
        # Update the deps file
        set(content "# generated by make process\nset(sources ${rel_deps})\n")
        update_file(${deps_file} "${content}")
        # Include the file so it's tracked as a generation dependency we don't
        # need the content.
        include(${deps_file})
    endfunction(update_deps_file)
    

    file(GLOB_RECURSE sources LIST_DIRECTORIES false *.h *.cpp)
    update_deps_file("${sources}")
    add_executable(test ${sources})
    

    程序中的唯一更改是在创建新文件之后。如果你不glob,工作流程就是修改 CMakeLists.txt 从Visual Studio内部进行重建。如果你做环球球,你就跑 cmake

    推荐文章