问题描述
我想从多个子目录中的对象创建一个库,每个对象都包含自己的CMakeLists.txt和OBJECT库技巧,以使多个目标具有不同的编译选项.
I would like to create a single library from objects from multiple sub-directories, each one containing their own CMakeLists.txt with OBJECT library trick to have multiple targets with different compile options.
以下是文件:
project_dir
|--- subdir1
| |--- src1.c
| |--- CMakeLists.txt
|--- subdir2
| |--- src2.c
| |--- CMakeLists.txt
|--- CMakeLists.txt
所有CMakeLists.txt的内容
Contents of all CMakeLists.txt
// project_dir/CMakeLists.txt
// what to put here? to make one single library (mainLib)
// project_dir/subdir1/CMakeLists.txt
add_library(lib11 OBJECT src1.c)
set_target_properties(lib11 PROPERTIES COMPILE_FLAGS "some-flags11")
add_library(lib12 OBJECT src1.c)
set_target_properties(lib12 PROPERTIES COMPILE_FLAGS "some-flags12")
// here I would like to add lib11:objects and lib12:objects to mainLib
// how should it be done?
// project_dir/subdir2/CMakeLists.txt
// *** similar to subdir1 but with src2.c that creates lib21 and lib22
// here I would like to add lib21:objects and lib22:objects to mainLib
// how should it be done?
可以独立完成平台吗?谢谢.
Can it be done platform independently? Thanks.
基于 CMake:如何从子项目的所有静态库中创建一个共享库?,我可以将cmake文件更改为以下内容,但仍然不能解决我的问题.
Based on CMake: how create a single shared library from all static libraries of subprojects?, I can change the cmake files to the following, but still doesn't solve my problem.
// project_dir/subdir1/CMakeLists.txt
add_library(lib11 OBJECT src1.c)
set_target_properties(lib11 PROPERTIES COMPILE_FLAGS "some-flags11")
add_library(lib12 OBJECT src1.c)
set_target_properties(lib12 PROPERTIES COMPILE_FLAGS "some-flags12")
add_library(lib1 STATIC $<TARGET_OBJECTS:lib11> $<TARGET_OBJECTS:lib12>)
// Above add_library cannot be OBJECT which would fix my problem.
// how to append the lib1 (lib11 and lib12) to mainLib?
更新我的帖子,尝试按照答案中的建议链接接口库.
Updating my post with attempt to chain interface libraries as suggested in the answer.
add_library(lib11 OBJECT test1/sub1/src1.c)
set_target_properties(lib11 PROPERTIES COMPILE_FLAGS "-DF1")
add_library(lib12 OBJECT test1/sub1/src1.c)
set_target_properties(lib12 PROPERTIES COMPILE_FLAGS "-DF2")
add_library(lib1 INTERFACE)
target_sources(lib1 INTERFACE $<TARGET_OBJECTS:lib11>)
target_sources(lib1 INTERFACE $<TARGET_OBJECTS:lib12>)
add_library(lib21 OBJECT test1/sub2/src2.c)
set_target_properties(lib21 PROPERTIES COMPILE_FLAGS "-DF1")
add_library(lib22 OBJECT test1/sub2/src2.c)
set_target_properties(lib22 PROPERTIES COMPILE_FLAGS "-DF2")
add_library(lib2 INTERFACE)
target_sources(lib2 INTERFACE $<TARGET_OBJECTS:lib21>)
target_sources(lib2 INTERFACE $<TARGET_OBJECTS:lib22>)
add_library(PARENT INTERFACE)
target_link_libraries(PARENT INTERFACE lib1)
target_link_libraries(PARENT INTERFACE lib2)
add_library(CORE OBJECT src.c)
add_library(GPARENT STATIC $<TARGET_OBJECTS:CORE>)
target_link_libraries(GPARENT INTERFACE PARENT)
推荐答案
几个 OBJECT 库可以合并到一个 INTERFACE 库中:
Several OBJECT libraries can be incorporated into one INTERFACE library:
# Create lib1 OBJECT library with some sources, compile flags and so.
add_library(lib1 OBJECT ...)
# Create lib2 OBJECT library with some sources, compile flags and so.
add_library(lib2 OBJECT ...)
# Create INTERFACE library..
add_library(libs INTERFACE)
# .. which combines OBJECT libraries
target_sources(libs INTERFACE $<TARGET_OBJECTS:lib1> $<TARGET_OBJECTS:lib2>)
结果库可以与标准target_link_libraries
一起使用:
Resulted library can be used with standard target_link_libraries
:
add_library(mainLib <some-source>)
target_link_libraries(mainLib libs)
可以进一步组合 INTERFACE 库:
# Create libs_another INTERFACE library like 'libs' one
add_library(libs_another INTERFACE)
..
# Combine 'libs' and 'libs_another' together
add_library(upper_level_libs INTERFACE)
target_link_libraries(upper_level_libs INTERFACE libs libs_another)
结果库的链"可以是任意长度.
Resulted libraries' "chain" could be of any length.
虽然这种组合OBJECT库的方式看起来很黑,但实际上 CMake文档:
While this way for combination of OBJECT libraries looks hacky, it is actually allowed by CMake documentation:
这篇关于CMake将来自不同CMakeLists.txt的对象附加到一个库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!