【问题标题】:Multiple directories under CMakeCMake下的多个目录
【发布时间】:2011-09-15 04:17:15
【问题描述】:

我目前正在使用递归 make 和 autotools,并希望迁移到 CMake 的项目看起来像这样:

lx/ (project root)
    src/
        lx.c (contains main method)
        conf.c
        util/
            str.c
            str.h
            etc.c
            etc.h
        server/
            server.c
            server.h
            request.c
            request.h
        js/
            js.c
            js.h
            interp.c
            interp.h
    bin/
        lx (executable)

我该怎么办?

【问题讨论】:

    标签: c build cmake


    【解决方案1】:

    如果从来没有任何高于 lx/src 目录的源,则不需要 lx/CMakeLists.txt 文件。如果有,它应该看起来像这样:

    cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
    project(lx)
    
    add_subdirectory(src)
    add_subdirectory(dir1)
    add_subdirectory(dir2)
    
    # And possibly other commands dealing with things
    # directly in the "lx" directory
    

    ...子目录按库依赖顺序添加。应该首先添加不依赖任何其他内容的库,然后添加依赖这些内容的库,依此类推。

    lx/src/CMakeLists.txt

    cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
    project(lx_exe)
    
    add_subdirectory(util)
    add_subdirectory(js)
    add_subdirectory(server)
    
    set(lx_source_files conf.c lx.c)
    add_executable(lx ${lx_source_files})
    
    target_link_libraries(lx server)
      # also transitively gets the "js" and "util" dependencies
    

    lx/src/util/CMakeLists.txt

    set(util_source_files
      etc.c
      etc.h
      str.c
      str.h
    )
    add_library(util ${util_source_files})
    

    lx/src/js/CMakeLists.txt

    set(js_source_files
      interp.c
      interp.h
      js.c
      js.h
    )
    add_library(js ${js_source_files})
    
    target_link_libraries(js util)
    

    lx/src/server/CMakeLists.txt

    set(server_source_files
      request.c
      request.h
      server.c
      server.h
    )
    add_library(server ${server_source_files})
    
    target_link_libraries(server js)
      # also transitively gets the "util" dependency
    

    然后,在命令提示符中:

    mkdir lx/bin
    cd lx/bin
    
    cmake ..
      # or "cmake ../src" if the top level
      # CMakeLists.txt is in lx/src
    
    make
    

    默认情况下,lx 可执行文件将使用这种精确的布局在“lx/bin/src”目录中结束。您可以使用 RUNTIME_OUTPUT_DIRECTORY 目标属性和 set_property 命令来控制它最终位于哪个目录。

    http://www.cmake.org/cmake/help/cmake-2-8-docs.html#prop_tgt:RUNTIME_OUTPUT_DIRECTORY

    http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:set_property

    如果库是通过 add_library 构建为 CMake 目标,则通过 CMake 目标名称引用 target_link_libraries 库,否则通过库文件的完整路径。

    另请参阅“cmake --help-command target_link_libraries”或任何其他 cmake 命令的输出,以及此处找到的 cmake 命令的完整在线文档:

    http://www.cmake.org/cmake/help/cmake-2-8-docs.html#section_Commands

    http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries

    【讨论】:

    • 非常感谢——但是获取“js”和“util”依赖项的“传递”是如何工作的?
    • 这是 CMake 的一个特性 - 它通过整个树跟踪 target_link_libraries 命令以在内部数据结构中构建依赖关系图,并使用它来生成正确的链接命令行。
    • 因为懒得列出文件,我发现 FILE(GLOB_RECURSE sourceVar ./ *.cpp *.c *.h) 非常有用。您还可以使用不递归到子子文件夹的 GLOB。否则,感谢 DLRdave 也让我开始使用 CMake。
    【解决方案2】:

    Steinberg VST3 库有一个可重用的方法,它递归地循环遍历子目录并在它们包含 CMakeLists.txt 文件时添加它们:

    # add every sub directory of the current source dir if it contains a CMakeLists.txt
    function(smtg_add_subdirectories)
        file(GLOB subDirectories RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
        foreach(dir ${subDirectories})
            if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
                if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${dir}/CMakeLists.txt")
                    add_subdirectory(${dir})
                endif()
            endif()
        endforeach(dir)
    endfunction()
    

    https://github.com/steinbergmedia/vst3_cmake/blob/master/modules/SMTG_AddSubDirectories.cmake

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多