I use CMake to build a project that consists of multiple nested static libraries .A similar but simple structure is shown in the figure below:
TestProject:
|-CMakeLists.txt
|-Main.cpp
|-level2
| | - level2.cpp
| | - level2.h
| | - CMakeLists.txt
| | - level1
| | |-level1.cpp
| | |-level1.h
| | |-CMakeLists.txt
| | |-third_party.lib-------(When I build level1.lib, I need to add dependence of this library.)
I use CMake to build static libraries for each level separately.I want to combine it with the library referenced by the previous layer when the static library of each layer is generated. For example, I build the static lib of level1 first, which depend on the existed third_party.lib.Then, in the CMakeLists.txt of level 2, I create the static library of level 2 depend on the static library of level 1 ( target_link_libraries(${PROJECT_NAME} LEVEL1) ), and then, I wanted to merge the libraries of level 2 and level 1 and third_party.lib together to a new static lib file named as level1_2.lib.
According to another similar question but different at the third_partyl.lib(CMake linking static libraries in different subdirectories into one single static library), I used object library, and get the merged static library.Here's my code:
#CMakeLists.txt in level1
cmake_minimum_required(VERSION 3.20.3)
#projcet name
project(LEVEL1 LANGUAGES CXX)
# Generate lib
add_library( LEVEL1obj OBJECT add.cpp)
# Add the include directories of user-written sources.
target_include_directories(LEVEL1obj PUBLIC ${PROJECT_SOURCE_DIR})
add_library(${PROJECT_NAME})
add_dependencies(${PROJECT_NAME} LEVEL1obj)
target_link_libraries(${PROJECT_NAME} PUBLIC LEVEL1obj)
And this is the CMakelists.txt of level2.
#CMakeLists.txt in level2
cmake_minimum_required(VERSION 3.20.3)
#projcet name
project(LEVEL2 LANGUAGES CXX)
add_subdirectory(level1)
add_library( LEVEL2obj OBJECT addplus.cpp addplus.h)
# Add the include directories of user-written sources.
target_include_directories(LEVEL2obj PUBLIC ${PROJECT_SOURCE_DIR})
target_link_libraries(LEVEL2obj PUBLIC LEVEL1obj)
add_library( ${PROJECT_NAME})
add_dependencies(${PROJECT_NAME} LEVEL1obj LEVEL2obj)
target_link_libraries(${PROJECT_NAME} PUBLIC LEVEL1obj LEVEL2obj)
This is the top level CMakeLists.txt in test project
cmake_minimum_required(VERSION 3.20.3)
project(TestCppLib)
file(GLOB SRC "${PROJECT_SOURCE_DIR}/*.cpp")
add_subdirectory(level2)
add_executable(${PROJECT_NAME} ${SRC})
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR})
add_dependencies(${PROJECT_NAME} LEVEL2)
target_link_libraries(${PROJECT_NAME} PRIVATE LEVEL2)
Now, I completed the compilation of level1-level2 projects with help. However, one problem is overlooked:I don't know how to deal with third-party libraries in level1.I don't know how to use the Object Library method to merge the third-party library with level1.lib.. I cannot use the method of building the object library first and then merging them. Because as far as I know, CMake may not have a method to convert a static library into an OBJECT library.
So, when I merge libraries, how should I deal with this third-party library problem?
Any help would be appreciated.