如何将这个包含多个源代码子目录的Make和Autotools项目迁移到CMake?

iibxawm4  于 2022-12-02  发布在  其他
关注(0)|答案(2)|浏览(129)

我目前正在使用递归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)

我该怎么做?

ymdaylpp

ymdaylpp1#

如果在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/源文件/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/源代码/实用程序/CMakeLists.txt

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

**文件名:

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

target_link_libraries(js util)

lx/源文件/服务器/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

5tmbdcev

5tmbdcev2#

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

相关问题