使用 VS2008 SP1 使用 CMake 2.8.x 将多个静态 .lib 文件链接到一个单一的 .lib 文件 [英] Linking multiple static .lib files into one monolithic .lib file using VS2008 SP1 using CMake 2.8.x

查看:22
本文介绍了使用 VS2008 SP1 使用 CMake 2.8.x 将多个静态 .lib 文件链接到一个单一的 .lib 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用cmake将目标文件链接到lib.xxxx.a文件相关,但不太一样,我使用 VS2008 SP1 使用 CMake 2.8.x 在 Windows 上构建了几个静态库.有没有办法单独通过 CMake 将所有现有静态库中的所有 .obj 文件重新链接到一个更大的整体库中,最好通过 add_library CMake 函数或其他类似的构造?

Related to using cmake to link object files into lib.xxxx.a file, but not quite the same thing, I have built several static libraries on Windows using CMake 2.8.x using VS2008 SP1. Is there a way via CMake alone to relink all of the .obj files inside all of those existing static libraries into one larger monolithic library, preferably via the add_library CMake function, or other similar construct?

我认为答案是否定的,所以我考虑过通过通常的 add_custom_command + add_custom_target 方法通过自定义命令滚动我自己的命令,它只是构造通过在调用 LINK.EXE 时提供所有其他库 .obj 文件来手动创建库.但我发现这种方法存在一些问题:

I think the answer is "no", and so I have thought about rolling my own via a custom command via the usual add_custom_command + add_custom_target approach, that simply constructs the library manually, by supplying all of the other libraries .obj files when calling LINK.EXE. But I see some problems with that approach:

  1. 我找不到指示 LINK.EXE 可执行文件的完全限定路径的 CMake 变量.然后,我必须以某种方式使用脆弱的启发式方法推导出 LINK.EXE 的路径:在不同的 Visual Studio 版本可能会找到 LINK.EXE 文件的意义上,它是脆弱的在不同的目录中,我需要它同时适用于 32 位和 64 位 Windows 编译器条件,并且能够适应 VS2008 和未来编译器修订版之间的升级.
  2. 我必须找到一种方法来查找其他静态库的所有 .obj 文件,在构建时间在 CMake 时间,因为在 CMake 时间.obj 文件当然不(总是)存在.出于构建性能的原因,我不想为了将它们添加到 LINK.EXE 命令行而诉诸于从 .lib 文件中提取 .obj 文件,因此 FILE(在这种情况下,GLOB...) 构造将是我最好的第二个选择.
  3. 可以通过以下方式简单地调用 LINK.EXE:LINK.EXE/OUT:monolithic.lib lib1.lib lib2.lib ...,但也许并非所有 .obj 都将被包括在内(我已经确认 LINK.EXE 省略了 lib1.lib lib2.lib ... 中的一些 .obj 文件,没有任何诊断消息解释原因,所以这种方法是不可能的);LINK.EXE 的在线文档在这一点上不清楚.任何人都有以这种方式使用 LINK.EXE 的经验吗?
  1. I could not find a CMake variable that indicates the fully-qualified path to the LINK.EXE executable. I would then have to somehow derive the path to LINK.EXE using a fragile heuristic: It is fragile in the sense that different Visual Studio versions may locate the LINK.EXE file in different directories, and I'm needing this to work for both 32-bit and 64-bit Windows compiler conditions, and be resilient against upgrades between VS2008 and future compiler revisions.
  2. I would have to find a way to find all of the .obj files of the other static libraries, at build time versus at CMake time, since at CMake time the .obj files of course do not (always) exist. For reasons of build performance, I desire not to resort to extracting the .obj files from the .lib files for the sake of adding them to the LINK.EXE command line, so a FILE(GLOB...) construct would be my best second alternative in this case.
  3. It may be possible to simply call LINK.EXE via: LINK.EXE /OUT:monolithic.lib lib1.lib lib2.lib ..., but maybe not all .obj's will be included ( I have confirmed that LINK.EXE omits some .obj files from lib1.lib lib2.lib ... without any diagnostic messages explaining why, so this approach is a non-starter); the online docs for LINK.EXE are unclear as to that point. Anyone have any experience with using LINK.EXE in that manner?

谢谢,

布伦特

P.S.,我知道如何使用 CMake 创建 DLL,但我现在特别不想诉诸于构建 DLL.

P.S., I know how to create a DLL using CMake, but I specifically do not want to resort to building a DLL at this point in time.

推荐答案

创建一个与虚拟源文件合并"的静态库,并将要合并的库添加到 STATIC_LIBRARY_FLAGS 中,因此它们将作为 lib.exe 的附加输入.

Create a static library "merged" with a dummy source file, and add libs to be merged to the STATIC_LIBRARY_FLAGS, so they will be additional input to lib.exe.

这将类似于:

ADD_LIBRARY(合并 STATIC dummy.c)

ADD_LIBRARY(merged STATIC dummy.c)

SET_TARGET_PROPERTIES(合并的属性
STATIC_LIBRARY_FLAGS "fullpath olib1.lib fullpath olib2.lib")

SET_TARGET_PROPERTIES(merged PROPERTIES
STATIC_LIBRARY_FLAGS "fullpath olib1.lib fullpath olib2.lib")

这种方法在 MySQL 内部使用,这里有一个更通用的宏来合并跨平台工作的静态库.它可以在这里找到 http://www.mail-archive.com/cmake@cmake.org/msg28670/libutils.cmake

This approach is used inside MySQL, there is a more general macro here to merge static libraries that works crosss-platform. It can be found here http://www.mail-archive.com/cmake@cmake.org/msg28670/libutils.cmake

这篇关于使用 VS2008 SP1 使用 CMake 2.8.x 将多个静态 .lib 文件链接到一个单一的 .lib 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆