【问题标题】:Build multiple examples with cmake使用 cmake 构建多个示例
【发布时间】:2017-11-23 20:44:12
【问题描述】:

我的项目使用相同的核心代码,我想构建如下结构

project
|
| - core_code
| - cmake 
| - CMakeLists.txt
| - example1
|     |
|     |- example1.cc
|     |- build
|
| - example2
|     |
|     |- example2.cc
|     |- build

我希望 cmake 在每个示例的构建子目录中创建目标。所以在我从projects 目录运行cmake 然后构建之后,结构应该是这样的:

project
    |
    | - core
    | - cmake 
    | - CMakeLists.txt
    | - example1
    |     |
    |     |- example1.cc
    |     |- build
    |          |- Makefile
    |          |- example1
    |
    | - example2
    |     |
    |     |- example2.cc
    |     |- build
    |          |- Makefile
    |          |- example2

project/CMakeLists.txt我该怎么办?

【问题讨论】:

    标签: cmake


    【解决方案1】:

    如果您的项目结构如下:

    project
        |
        |- CMakeLists.txt
        |- core
        |    |
        |    |- core.cc
        |    |- core.h
        |    |- CMakeLists.txt
        |
        |- example1
        |    |
        |    |- example1.cc
        |    |- CMakeLists.txt
        |
        |- example2
             |
             |- example2.cc
             |- CMakeLists.txt
    

    您的 project/CMakeLists.txt 文件只包含其他 cmakelists 文件

    project/CMakeLists.txt:

    cmake_minimum_required (VERSION 3.5)
    project (project CXX C)
    
    add_subdirectory(core)
    add_subdirectory(example1)
    add_subdirectory(example2)
    

    您的 core/CMakeLists.txt 文件构建核心目标

    project/core/CMakeLists.txt:

    add_library(core core.cc)
    target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
    

    您的 exampleN/CMakeLists.txt 文件构建示例目标

    project/example1/CMakeLists.txt:

    add_executable(example1 example.cc)
    target_link_libraries(example1 core)
    

    当您生成构建文件时,cmake 会在构建目录中模仿您的目录结构,因此运行以下命令将导致以下目录结构:

    $ cd project
    $ mkdir build
    $ cd build
    $ cmake ..
    

    生成的目录结构如下所示:

    project
        |
        |- CMakeLists.txt
        |- core
        |    |
        |    |- core.cc
        |    |- core.h
        |    |- CMakeLists.txt
        |
        |- example1
        |    |
        |    |- example1.cc
        |    |- CMakeLists.txt
        |
        |- example2
        |    |
        |    |- example2.cc
        |    |- CMakeLists.txt
        |                                  
        |- build
             |
             |- Makefile
             |- core
             |     |- Makefile
             |
             |- example1
             |     |- Makefile
             |     |- example1
             |
             |- example2
                   |- Makefile
                   |- example2
    

    现在如果你只想构建example2,例如,你可以执行以下命令:

    $ cd project/build/example2
    $ make
    

    这样做的好处是它不会用构建文件污染您的源代码树。想炸掉build目录,只需要删除一个目录

    $ rm -r project/build
    

    【讨论】:

      【解决方案2】:

      假设您在某处有其他 CMakeLists.txt,例如在cmake/example/CMakeLists.txt,内容如下:

      add_executable(${EXAMPLE_NAME} ${EXAMPLE_DIR}/${EXAMPLE_NAME}.cc)
      

      您可以通过project/CMakeLists.txt 调用它:

      # Build the first example in the 'example1/build' subdirectory
      set(EXAMPLE_NAME example1)
      set(EXAMPLE_DIR ${CMAKE_SOURCE_DIR}/example1)
      add_subdirectory(cmake/example ${CMAKE_SOURCE_DIR}/example1/build)
      # Build the second example in the 'example2/build' subdirectory
      set(EXAMPLE_NAME example2)
      set(EXAMPLE_DIR ${CMAKE_SOURCE_DIR}/example2)
      add_subdirectory(cmake/example ${CMAKE_SOURCE_DIR}/example2/build)
      

      请注意,CMake 仅在 build 目录 中生成 Makefile,而更改 build 目录的唯一方法是 add_subdirectory() 调用。所以如果没有额外的CMakeLists.txt,你就不能在顶级目录中获得Makefile。但是您可以为多个构建目录使用一个额外的 CMakeLists.txt


      确切地说,您也可以使用顶级CMakeLists.txt作为示例,但您需要使其“可重入”,这对我来说似乎很难看。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-05
        • 1970-01-01
        • 2017-01-15
        • 1970-01-01
        • 2013-06-07
        • 1970-01-01
        • 1970-01-01
        • 2020-01-08
        相关资源
        最近更新 更多