问题描述
我想控制在CMake中找到/与我的二进制文件链接的库的类型。最终目标是,生成二进制文件尽可能静态,即静态链接到具有静态版本的每个库。这是重要的,因为它将使测试期间跨不同系统的二进制文件的可移植性。
ATM这似乎很难实现作为FindXXX.cmake包,或更准确地说find_library命令总是拾取动态库,无论静态和动态都可用。
有关如何实施此功能的提示(最好是以优雅的方式)非常受欢迎!
我做了一些调查,虽然我找不到满意的解决方案的问题,我找到一个半解决方案。
静态构建的问题归结为三件事:
-
构建和链接项目的内部库。
很简单,只需要翻转
BUILD_SHARED_LIBS
开关关
。 -
查找外部库的静态版本。
唯一的方法似乎是设置
CMAKE_FIND_LIBRARY_SUFFIXES
以包含所需的文件后缀)。
这个解决方案是一个肮脏的,非常反对CMake的跨平台愿望。 IMHO这应该由CMake在幕后处理,但据我所知,由于Windows上的.lib混乱,似乎CMake开发人员更喜欢当前的实现。
-
静态链接到系统库。
CMake提供了一个选项,其中基于文档:结束链接行,使静态系统库用过的。
一个会想,这是它,问题解决了。但是,似乎目前的实现不能胜任这项任务。如果该选项被打开,CMake将生成一个隐式链接器调用,其参数列表以传递给链接器的选项结尾,包括 -Wl,-Bstatic
。然而,这还不够。只有指示链接器静态链接才会导致错误,在我的情况下: / usr / bin / ld:找不到-lgcc_s
。缺少的是告诉gcc,我们需要通过 -static
参数的静态链接,这是由CMake生成的链接器调用 。我认为这是一个错误,但我还没有成功得到来自开发人员的确认。最后,我认为所有这一切都可以而且应该由CMake在幕后完成,毕竟它不那么复杂,除非它在Windows上不可能 - 如果计数为复杂...
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 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:
Building and linking the project's internal libraries.
Pretty simple, one just has to flip the
BUILD_SHARED_LIBS
switchOFF
.Finding static versions of external libraries.
The only way seems to be setting
CMAKE_FIND_LIBRARY_SUFFIXES
to contain the desired file suffix(es) (it's a priority list).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.
Linking statically against system libraries.
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.
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:如何产生二进制“尽可能静态”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!