在cmake的多个目录 [英] multiple directories under cmake

查看:311
本文介绍了在cmake的多个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用递归的品牌和自动工具,我期待迁移到CMake的一个项目,看起来是这样的:

I'm currently using recursive make and autotools and am looking to migrate to CMake for a project that looks something like this:

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)

我应该如何去吗?

How should I go about this?

推荐答案

如果有永远比LX / src目录较高任何来源,那么就没有必要在 LX /的CMakeLists.txt 文件。如果有,就应该是这个样子:

If there's never any source higher than the lx/src directory, then there's no need for the lx/CMakeLists.txt file. If there is, it should look something like this:

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

...其中子目录在库的依赖顺序添加。依赖于没有别的应先加入库,然后依赖于那些,等等库

...where the subdirectories are added in library dependency order. Libraries that depend on nothing else should be added first, and then libraries that depend on those, and so on.

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目录/服务器/的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

然后,在命令提示符:

Then, in a command prompt:

mkdir lx/bin
cd lx/bin

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

make

默认情况下,LX可执行文件将在LX /斌/ src目录使用这个准确布局目录中结束。你可以控制它通过使用RUNTIME_OUTPUT_DIRECTORY目标属性和set_property命令结束哪个目录下了。

By default, the lx executable will end up in the "lx/bin/src" directory using this exact layout. You can control what directory it ends up in by using the RUNTIME_OUTPUT_DIRECTORY target property and the set_property command.

<一个href=\"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#prop_tgt:RUNTIME_OUTPUT_DIRECTORY

<一个href=\"http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:set_property\">http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:set_property

请参阅target_link_libraries库或者通过CMake的目标名称,如果LIB被构建为目标的CMake通过add_library,或通过完整路径库文件,否则。

Refer to target_link_libraries libs either by CMake target name, if the lib is built as a CMake target via add_library, or by full path to the library file otherwise.

另见的CMake的--help命令target_link_libraries的输出,或任何其他CMake的命令,并在这里找到了cmake的命令完整的在线文档:

See also, the output of "cmake --help-command target_link_libraries", or any other cmake command, and the full online documentation for cmake commands found here:

<一个href=\"http://www.cmake.org/cmake/help/cmake-2-8-docs.html#section_Commands\">http://www.cmake.org/cmake/help/cmake-2-8-docs.html#section_Commands

<一个href=\"http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries\">http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries

这篇关于在cmake的多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆