这是项目树:它是一个使用另一个静态库的静态库
Static_lib/
|
----src/
|----src1.cpp
|----src2.cpp
----inc/
|----head1.h
|----head2.h
----Static_lib2/
|----lib/
|----32/
|Debug/
lib2.lib
|Release/
lib2.lib
|----64/
|Debug/
lib2.lib
|Release/
lib2.lib
|----include/
|bunch of headers
----build/
----CMakeLists.txt
这是我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.5.1)
project(Static_lib)
set(SOURCE_FILES src/src1.cpp src/src2.cpp)
set(HEADER_FILES inc/head1.h inc/head2.h)
add_library(Static_lib STATIC ${SOURCE_FILES} ${HEADER_FILES})
target_include_directories(Static_lib PUBLIC
${CMAKE_SOURCE_DIR}/inc
${CMAKE_SOURCE_DIR}/Static_lib2/include
)
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS FALSE)
else(CMAKE_SIZEOF_VOID_P EQUAL 8)
set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS TRUE)
endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
find_library(STATIC_LIB2 libcrypto.a)
target_link_libraries(Static_lib "${STATIC_LIB2}")
elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
target_link_libraries(Static_lib debug ${CMAKE_SOURCE_DIR}/Static_lib2/lib/64/Debug/lib2.lib)
target_link_libraries(Static_lib optimized ${CMAKE_SOURCE_DIR}/Static_lib2/lib/64/Release/lib2.lib)
else(CMAKE_SIZEOF_VOID_P EQUAL 8)
target_link_libraries(Static_lib debug ${CMAKE_SOURCE_DIR}/Static_lib2/lib/32/Debug/lib2.lib)
target_link_libraries(Static_lib optimized ${CMAKE_SOURCE_DIR}/Static_lib2/lib/32/Release/lib2.lib)
endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
endif(CMAKE_SYSTEM_NAME STREQUAL Linux)
当我使用Visual Studio生成器创建项目时,创建的项目没有错误或警告,但是项目属性中没有依赖项和库。
在构建之后,我得到的库实际上没有它应该具有的大小。
有人可以提供一些见解吗?也许我的CMakeLists代码错了?
提前致谢!
答案 0 :(得分:0)
我刚刚在cmake: include library dependencies in static lib
找到答案默认情况下,静态库不会合并在一起。 手动完成是可行的方法。