【问题标题】:CMake: Build Multiple Executables in one Project with Static LibraryCMake:使用静态库在一个项目中构建多个可执行文件
【发布时间】:2014-05-15 17:16:36
【问题描述】:

我正在开发一个项目,该项目由 3 个服务器可执行文件和一个用于共享代码的库组成。我希望它是跨平台的,所以我使用 CMake(因为 Xcode 无论如何都很痛苦)来处理构建过程。我在设置 CMakeLists 时遇到问题,以便在构建可执行文件时可以从同一级别的目录中包含库。

这是目录结构(和 CMake 文件):

tethealla2.0/
    CMakeLists.txt
    libtethealla/
        CMakeLists.txt
        encryption/
        utils/
    patch_server/
        CMakeLists.txt
    login_server/
        CMakeLists.txt
    ship_server/
        CMakeLists.txt

我的顶级CMake(tethealla2.0/CMakeLists.txt,只包含应该编译的子项目):

project(tethealla CXX)
cmake_minimum_required(VERSION 2.6)

add_subdirectory(libtethealla)
add_subdirectory(patch_server)

tethealla2.0/libtethealla/CMakeLists.txt,生成静态库:

project(Libtethealla C)
cmake_minimum_required(VERSION 2.6)

include_directories(encryption)

set(ENC_DR encryption/)

set(ENCRYPTION_SOURCES 
  ${ENC_DR}/psobb-crypt.c
  ${ENC_DR}/psogc-crypt.c
  ${ENC_DR}/psobb-crypt.c
  ${ENC_DR}/encryption.c
  )

add_library(tethealla STATIC ${ENCRYPTION_SOURCES})

tethealla2.0/patch_server/CMakeLists.txt 到目前为止:

project(patch_server CXX)
cmake_minimum_required(VERSION 2.6)

add_executable(server main.cc)
target_link_libraries(server tethealla)

因此,如果我从顶层构建它会更有意义,因为 tethealla2.0/CMakeLists.txt 将从每个子目录继承目标,而 patch_server 中的目标将可以访问 tethealla 库。但是,我想要的是能够从这些子目录中构建以生成 Xcode 项目,以便我可以单独处理/重新编译它们。为此,我需要能够访问 libtethealla/build 目录(CMake 输出的位置)以从 patch_server 访问 libtethealla.a 库。这可能吗?

另一方面,即使从顶级目录构建,patch_server 中的源代码也不能包含“encryption.h”,即加密库的头文件。这似乎建设得很好。对此的任何想法也非常感谢!

【问题讨论】:

    标签: c++ cmake


    【解决方案1】:

    我的解决方案是使用 add_subdirectory 和 shared_lib 目录的相关补丁。我不认为这是一个完美的解决方案,它有一些注意事项:

    • 必须将与标头保护非常相似的逻辑添加到库 CMakeLists.txt 中,以防止多次定义目标。
    • 每个 CMakeList.txt 文件都必须知道库的相对路径,如果要移动库,则必须更新所有 CMakeList。

    假设目录结构如下所示:

    root/
        CMakeLists.txt
        shared_lib/
            CMakeLists.txt
            inc/
                foo.h
            src/
                foo.c
        exec1/
           CMakeLists.txt
           main.c
        exec2/
           CMakeLists.txt
           main.c
    

    root/CMakeList.txt

    cmake_minimum_required(VERSION 2.6)
    
    add_subdirectory(shared_lib)
    
    add_subdirectory(exec1)
    add_subdirectory(exec2)
    

    我决定 shared_lib/CMakeLists.txt 将导出一个名为 SHARED_DIR_INCLUDE_DIR 的变量。这种方法有助于稍微解耦。

    root/exec1/CMakeLists.txt

    cmake_minimum_required(VERSION 2.6)
    
    add_subdirectory(./../shared_lib shared_lib)
    
    include_directories(${SHARED_LIB_INCLUDE_DIR})
    
    set(SRCS main.c)
    add_executable(exec1 ${SRCS})
    target_link_libraries(exec1 shared_lib)
    

    第四行的if()解决了多次添加CMakeLists文件时target的多重定义问题。第二行和第三行导出SHARED_LIB_INCLUDE_DIR中库的include目录

    root/shared_lib/CMakeLists.txt

    cmake_minimum_required(VERSION 2.6)
    
    set(SHARED_LIB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/inc)
    
    set(SHARED_LIB_INCLUDE_DIR ${SHARED_LIB_INCLUDE_DIR} PARENT_SCOPE)
    
    if(TARGET shared_lib)
    
    message("shared_lib is already defined")
    
    else()
    
    include_directories(${SHARED_LIB_INCLUDE_DIR})
    
    set(LIB_SRCS ./src/foo.c)
    
    add_library(shared_lib STATIC ${LIB_SRCS})
    
    endif()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多