CMake:使用静态库在一个项目中构建多个可执行文件

CMake: Build Multiple Executables in one Project with Static Library

本文关键字:一个 项目 构建 可执行文件 静态 CMake      更新时间:2023-10-16

我正在做一个由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/cmakelsts .txt将从每个子目录继承目标,而patch_server中的目标将有权访问tethealla库。然而,我想要的是能够从这些子目录中生成Xcode项目,以便我可以单独工作/重新编译它们。要做到这一点,我需要能够得到libtethealla/构建目录(其中CMake输出)访问libtethealla。一个来自patch_server的库。这可能吗?

在另一种注意事项上,即使在顶层目录中构建,我的源代码在patch_server中也不能包含"encryption.h",加密库的头文件。看起来建得很好。任何关于这一点的想法也非常感谢!

我的解决方案是使用add_subdirectory与shared_lib目录的相对补丁。我不认为这是一个完美的解决方案,它有它的警告:

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

我们假设目录结构是这样的:

root/
    CMakeLists.txt
    shared_lib/
        CMakeLists.txt
        inc/
            foo.h
        src/
            foo.c
    exec1/
       CMakeLists.txt
       main.c
    exec2/
       CMakeLists.txt
       main.c
根/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的变量。这种方法有助于解耦一些东西。

根/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)
在CMakeLists文件被多次添加的情况下,第四行的

if()解决了目标的多个定义的问题。第二行和第三行导出SHARED_LIB_INCLUDE_DIR

中库的include目录。根/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()
相关文章: