如何在 Windows 的 CMake 中将多个静态库捆绑到单个库中 [英] How to bundle multiple static libraries into single library in CMake for windows
问题描述
我想在 CMake 中将五个静态库捆绑到一个库中.我该如何继续?
I want to bundle five static libraries into one library in CMake. How can I proceed for this?
像库 a、b、c、d 和 e 应该捆绑到 alpha_lib
中.
Like library a, b, c, d, and e should bundle into alpha_lib
.
推荐答案
如果您使用的是 Visual Studio,则可以利用 Microsoft 库管理器 (LIB.exe) 将您的静态库合并为一个.您的 CMake 可以按照以下步骤操作:
If you are using Visual Studio, you can take advantage of the Microsoft Library Manager (LIB.exe) to combine your static libraries into one. Your CMake could follow these steps:
使用
find_program()
让 CMake 在您的系统上找到 MSVClib.exe
工具.如果从 Visual Studio 命令提示符运行cmake
,find_program
可以自动定位lib.exe
,而无需使用可选的PATHS
参数告诉它去哪里找.
Use
find_program()
to have CMake locate the MSVClib.exe
tool on your system. If you runcmake
from the Visual Studio Command Prompt,find_program
can locatelib.exe
automatically, without using the optionalPATHS
argument to tell it where to look.
使用 CMake 的 add_custom_target()
命令使用合并库的语法调用lib.exe
:
Use CMake's add_custom_target()
command to call lib.exe
using the syntax for merging libraries:
lib.exe /OUT:alpha_lib.lib a.lib b.lib c.lib d.lib e.lib
你可以使用依赖于目标的生成器表达式 在自定义目标命令中让 CMake 解析您构建的库的位置.自定义目标将在您的 Visual Studio 解决方案中创建一个项目,该项目可以单独运行以将所有构建的静态库合并到一个库中.
You can use target-dependent generator expressions in the custom target command to have CMake resolve the locations of your built libraries. The custom target will create a Project in your Visual Studio solution that can be run separately to merge all of the built static libraries into one library.
您的 CMake 可能如下所示:
Your CMake could look something like this:
# Create the static libraries (a, b, c, d, and e)
add_library(a STATIC ${a_SOURCES})
...
add_library(e STATIC ${e_SOURCES})
# Tell CMake to locate the lib.exe tool.
find_program(MSVC_LIB_TOOL lib.exe)
# If the tool was found, create the custom target.
if(MSVC_LIB_TOOL)
add_custom_target(CombineStaticLibraries
COMMAND ${MSVC_LIB_TOOL} /OUT:$<TARGET_FILE_DIR:a>/alpha_lib.lib
$<TARGET_FILE:a>
$<TARGET_FILE:b>
$<TARGET_FILE:c>
$<TARGET_FILE:d>
$<TARGET_FILE:e>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
endif()
这篇关于如何在 Windows 的 CMake 中将多个静态库捆绑到单个库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!