CMake:使用静态库在一个项目中构建多个可执行文件
CMake: Build Multiple Executables in one Project with Static Library
我正在做一个由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
的变量。这种方法有助于解耦一些东西。
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
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()
- 运行同一解决方案的另一个项目的项目
- 从链接列表c++中删除一个项目
- 仅从无序集合中删除一个项目
- 是否可以将 EXE 文件作为 lib 文件链接到另一个项目?
- 如何使用另一个项目命名空间?
- 如何在不创建新配置的情况下对两个不同解决方案使用的一个项目使用不同的 #defines
- 数组中无法解释的最后一个项目
- 单元测试类无法在我要测试的同一解决方案中的另一个项目中找到类
- 有没有更好的方法来检测向量中一个项目的多次出现?
- 是否有其他方法将.dll文件从一个项目复制到我的启动项目中的可执行文件旁边
- 如果我尝试将对话框从一个项目移动到另一个项目,我是否需要从 rc 文件中复制 DESIGNINFO、对话框信息和AFX_
- OpenCV:文件存储操作员">>"在一个项目中工作,但不在另一个项目中工作(在同一类上)
- 如何将另一个项目中用 C 编写的源代码包含在我自己的项目中,C++在 Visual Studio 中
- 什么 STL 算法可以确定容器中的一个项目是否满足谓词?
- 将过滤器中的所有.cpp和.h移动到另一个项目并更正其文件夹位置
- 在另一个项目中包括继承的类时,抽象类的链接器错误
- 创建在另一个项目中定义的类对象
- 我正在尝试在Eclipse中构建一个C 项目,但是即使在使用MINGW路径设置路径变量之后,也会获得构建错误
- 在包含相同项目的向量中删除一个项目
- 将多个原型文件包含在一个项目中会导致protobuf_AssignDescriptorsOnce()已经有一个主体