我有2个项目(prj1和prj2)。一个(prj2)依赖于另一个(prj1)是静态库。我到达时用CMake分别编译它们。

但是我需要将一个(prj1)集成到另一个(prj2)。所以我想CMake先编译静态库(prj1),然后再链接静态库。我尝试了一些操作,但id无效。

  • prj1“核心”是https://gitlab.com/RyDroid/PlanetWars2dRT-core
  • prj2“SDL2”是https://gitlab.com/RyDroid/PlanetWars2dRT-SDL2/(请参阅分支添加核)

  • 在prj2中,externals / core是git子模块(对于非git用户,您可以将此目录视为prj1的复制粘贴)。
    我在prj2“SDL2”中尝试了此CMakeLists.txt(但没有成功):
    cmake_minimum_required(VERSION 2.8)
    
    
    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
        "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
      set(CMAKE_CXX_FLAGS       "-Wall -Wextra -Wformat=2 -Wpedantic -D_FORTIFY_SOURCE=2")
      set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
    elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
      # TODO
    endif()
    
    set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
    # if (CMAKE_VERSION VERSION_LESS "3.1")
    #   if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
    #       CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    #     set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
    #   endif()
    # else()
    #   set(CMAKE_CXX_STANDARD 11)
    # endif()
    
    project(PlanetWars2dRT-SDL2)
    
    # Version number
    set(VERSION_MAJOR "0")
    set(VERSION_MINOR "0")
    set(VERSION_MICRO "0")
    
    # Configure a header file to pass some of the CMake settings
    # to the source code.
    configure_file (
      "src/compilation_config.h.in"
      "${PROJECT_BINARY_DIR}/compilation_config.h"
      )
    # Add the binary tree to the search path for include files
    # so that we will find compilation_config.h
    include_directories("${PROJECT_BINARY_DIR}")
    
    set(LIBRARY_OUTPUT_PATH    lib/${CMAKE_BUILD_TYPE})
    set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})
    
    include_directories(src/)
    file(
      GLOB_RECURSE
      source_files
      src/*
      )
    add_executable(planet-wars-2d-rt-sdl2 ${source_files})
    
    #include(ExternalProject)
    #ExternalProject_Add(PlanetWars2dRT PREFIX externals/core/)
    include_directories(externals/core/src/utils/ externals/core/src/specific/)
    add_subdirectory(externals/core/)
    #find_package(PlanetWars2dRT-core REQUIRED)
    
    include(FindPkgConfig)
    pkg_search_module(SDL2 REQUIRED sdl2)
    pkg_search_module(SDL2GFX REQUIRED SDL2_gfx)
    include_directories(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS})
    target_link_libraries(
      planet-wars-2d-rt-sdl2
      planet-wars-2d-rt-core
      ${SDL2_LIBRARIES} ${SDL2GFX_LIBRARIES}
      )
    

    那是prj2树的简化版本:
    .
    ├── build (with CMake stuff generated with "cmake ..")
    ├── CMakeLists.txt
    ├── externals
    │   └── core
    │       ├── build (with CMake stuff generated with "cmake ..")
    │       ├── CMakeLists.txt
    │       ├── makefile
    │       └── src
    ├── makefile
    ├── README.md
    └── src
        ├── compilation_config.h.in
        └── planet-wars-2d-rt-sdl2.cpp
    

    如何使用CMake编译prj2“SDL2”中的prj1“core”库,然后将prj1的库与prj2(再次使用CMake)链接?

    如果您的解决方案不适用于非GNU / Linux操作系统,则不是大问题。注意:我的PC在Debian GNU / Linux 8“Jessie”上运行。

    问候。

    最佳答案

    我无法访问您的repo https://gitlab.com/RyDroid/PlanetWars2dRT-SDL2/,也许服务器已关闭以进行维护。如果我正确理解了您的问题,则可以创建如下结构:

    根/

  • CMakeLists.txt // 1
  • src /
  • CMakeLists.txt // 2
  • main.cpp
  • SDL2 /
  • 一些来源
  • CMakeLists.txt // 3
  • 核心/
  • 一些来源
  • CMakeLists.txt // 4

  • 在CMakeLists.txt [1]中,您应该声明项目名称,所需的软件包,通用标志,包含路径等:
    cmake_minimum_required( VERSION 2.x )
    project(name)
    
    include_directories(inc inc/core inc/SDL2 inc/SthElse)
    
    add_subdirectory(src)
    

    在子目录src的CMakeLists.txt [2]中,您应该声明您的主可执行文件,还应在prj1和prj2中添加子目录
     add_subdirectory(core)
     add_subdirectory(SDL2)
     add_executable(main main.cpp)
     target_link_libraries(main core SDL2 SomeOtherLib)
    

    最后,在lib目录中的CMakeLists.txt [3] && [4]中,您应该声明静态库:
     add_library(core STATIC ${YourSourceFiles})
    

    这种方法一直对我有用。如果您曾经将“core”和“SDL2”作为独立的二进制文件进行编译和运行,那么也许您将不得不对其进行一些重组。

    07-28 10:31