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

电脑杂谈  发布时间:2020-05-24 21:06:37  来源:网络整理

cmake 静态库_cmake 静态库_cmake 引用静态库

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

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

cmake 引用静态库_cmake 静态库_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,仅包含应编译的子项目):

cmake 静态库_cmake 引用静态库_cmake 静态库

project(tethealla CXX)
cmake_minimum_required(VERSION 2.6)
add_subdirectory(libtethealla)
add_subdirectory(patch_server)

tethealla2.0 / libtethealla / CMakeLists.txt,它会生成一个静态库:

cmake 静态库_cmake 静态库_cmake 引用静态库

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:

cmake 静态库_cmake 引用静态库_cmake 静态库

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项目,以便我可以分别处理/重新编译它们. 为此cmake 静态库,我需要能够访问libtethealla / build目录(CMake输出)以从patch_server访问libtethealla.a库. 这可能吗?

还请注意,即使在顶层目录的构建中,我的patch_server中的源也不能包含“ encryption.h”cmake 静态库,这是加密库的头文件. 看起来不错任何对此的想法也非常感谢!


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-220914-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...