我目前正在使用递归make和自动工具,并希望为一个类似于以下内容的项目迁移到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)

我该怎么做呢?

推荐答案

如果没有比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 target属性和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_library库,否则可以通过库文件的完整路径引用.

另请参见"cmake--help-command target_link_library"或任何其他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

C++相关问答推荐

segfault在C中使用getline()函数

从C函数调用asm函数时生成错误的BLX指令(STM32H753上的gcc)

GCC:try 使用—WError或—pedantic using pragmas

C中的__attributor__((aligned(4),packed))与 struct 的用法

在函数中使用复合文字来初始化C语言中的变量

在C中将通用字符名称转换为UTF-8

如何将字符串传递给函数并返回在C中更改的相同字符串?

如何在POSIX-UEFI中获得输入?

MacOS下C++的无阻塞键盘阅读

可变宏不能编译

按长度对argv中的单词进行排序

C语言中奇怪的输出打印数组

如何将大写/小写土耳其字母相互转换?

用于计算位数和的递归C函数

在printf()中用%.*S格式填充长度为0的字符串是否会调用任何UB?如果是,是哪一个?

C语言中的指针和多维数组

atoi函数最大长-长误差的再创造

我可以使用Windows SDK';s IN6_IS_ADDR_LOOPBACK等,尽管没有文档?

gdb - 你能找到持有内部 glibc 锁的线程吗?

当 n 是我们从用户那里获得的整数时,创建 n 个 struct 参数