使用 CMake 2.8.x 使用 VS2008 SP1 将多个静态 .lib 文件链接到一个整体 .lib 文件

发布于 2024-10-06 23:53:41 字数 1220 浏览 0 评论 0原文

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

我认为答案是“否”,因此我考虑过通过通常的 add_custom_command + add_custom_target 方法通过自定义命令自行推出,该方法只需手动构建库,通过在调用 LINK.EXE 时提供所有其他库 .obj 文件。但我发现这种方法存在一些问题:

  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的经验吗?

谢谢,

Brent

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

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?

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. 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 (EDIT: 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?

Thanks,

Brent

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-10-13 23:53:41

创建一个与虚拟源文件“合并”的静态库,并将要合并的库添加到 STATIC_LIBRARY_FLAGS,这样它们将成为 lib.exe 的附加输入。

这将类似于:

ADD_LIBRARY(merged STATIC dummy.c)

SET_TARGET_PROPERTIES(merged PROPERTIES
STATIC_LIBRARY_FLAGS "full\path\to\lib1.lib full\path\to\lib2.lib")

这种方法在MySQL内部使用,这里有一个更通用的宏来合并跨平台工作的静态库。它可以在这里找到 http://www.mail-archive。 com/[电子邮件受保护]/msg28670/libutils.cmake

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.

This would be something like:

ADD_LIBRARY(merged STATIC dummy.c)

SET_TARGET_PROPERTIES(merged PROPERTIES
STATIC_LIBRARY_FLAGS "full\path\to\lib1.lib full\path\to\lib2.lib")

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/[email protected]/msg28670/libutils.cmake

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文