CMake:如何生成“尽可能静态"的二进制文件; [英] CMake: how to produce binaries "as static as possible"

查看:40
本文介绍了CMake:如何生成“尽可能静态"的二进制文件;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想控制在 CMake 中找到/链接到我的二进制文件的库的类型.最终目标是生成二进制文件尽可能静态",即静态链接每个具有可用静态版本的库.这一点很重要,因为它将在测试期间实现跨不同系统的二进制文件的可移植性.

I would like to have control over the type of the libraries that get found/linked with my binaries in CMake. The final goal is, to generate binaries "as static as possible" that is to link statically against every library that does have a static version available. This is important as would enable portability of binaries across different systems during testing.

ATM 这似乎很难实现,因为 FindXXX.cmake 包,或者更准确地说,当静态和动态都可用时,find_library 命令总是选择动态库.

ATM this seems to be quite difficult to achieve as the FindXXX.cmake packages, or more precisely the find_library command always picks up the dynamic libraries whenever both static and dynamic are available.

非常欢迎提供有关如何实现此功能的提示(最好以优雅的方式)!

Tips on how to implement this functionality - preferably in an elegant way - would be very welcome!

推荐答案

我做了一些调查,虽然我没有找到令人满意的解决方案,但我确实找到了一个半解决方案.

I did some investigation and although I could not find a satisfying solution to the problem, I did find a half-solution.

静态构建的问题归结为三件事:

The problem of static builds boils down to 3 things:

  1. 构建和链接项目的内部库.

  1. Building and linking the project's internal libraries.

很简单,只需拨动BUILD_SHARED_LIBS 开关OFF.

Pretty simple, one just has to flip the BUILD_SHARED_LIBS switch OFF.

查找外部库的静态版本.

Finding static versions of external libraries.

唯一的方法似乎是将 CMAKE_FIND_LIBRARY_SUFFIXES 设置为包含所需的文件后缀(这是一个优先级列表).

The only way seems to be setting CMAKE_FIND_LIBRARY_SUFFIXES to contain the desired file suffix(es) (it's a priority list).

这个解决方案是一个相当肮脏"的解决方案,并且非常违背 CMake 的跨平台愿望.恕我直言,这应该由 CMake 在幕后处理,但据我了解,由于 Windows 上的.lib"混淆,CMake 开发人员似乎更喜欢当前的实现.

This solution is quite a "dirty" one and very much against CMake's cross-platform aspirations. IMHO this should be handled behind the scenes by CMake, but as far as I understood, because of the ".lib" confusion on Windows, it seems that the CMake developers prefer the current implementation.

静态链接到系统库.

CMake 提供了一个选项LINK_SEARCH_END_STATIC 基于文档:结束一个链接行,以便使用静态系统库."有人会想,就是这样,问题就解决了.但是,目前的实现似乎无法胜任这项任务.如果打开该选项,CMake 会生成一个隐式链接器调用,其参数列表以传递给链接器的选项结尾,包括 -Wl,-Bstatic.然而,这还不够.仅指示链接器静态链接会导致错误,在我的情况下:/usr/bin/ld: cannot find -lgcc_s.缺少的是告诉 gcc 我们需要通过 -static 参数进行静态链接,该参数是 not 生成到 CMake 的链接器调用的.我认为这是一个错误,但我还没有设法得到开发人员的确认.

CMake provides an option LINK_SEARCH_END_STATIC which based on the documentation: "End a link line such that static system libraries are used." One would think, this is it, the problem is solved. However, it seems that the current implementation is not up to the task. If the option is turned on, CMake generates a implicit linker call with an argument list that ends with the options passed to the linker, including -Wl,-Bstatic. However, this is not enough. Only instructing the linker to link statically results in an error, in my case: /usr/bin/ld: cannot find -lgcc_s. What is missing is telling gcc as well that we need static linking through the -static argument which is not generated to the linker call by CMake. I think this is a bug, but I haven't managed to get a confirmation from the developers yet.

最后,我认为这一切都可以而且应该由 CMake 在幕后完成,毕竟这并不复杂,除了在 Windows 上是不可能的——如果这算复杂的话......

Finally, I think all this could and should be done by CMake behind the scenes, after all it's not so complicated, except that it's impossible on Windows - if that count as complicated...

这篇关于CMake:如何生成“尽可能静态"的二进制文件;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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