1. <small id='sWxef'></small><noframes id='sWxef'>

      <bdo id='sWxef'></bdo><ul id='sWxef'></ul>

  2. <i id='sWxef'><tr id='sWxef'><dt id='sWxef'><q id='sWxef'><span id='sWxef'><b id='sWxef'><form id='sWxef'><ins id='sWxef'></ins><ul id='sWxef'></ul><sub id='sWxef'></sub></form><legend id='sWxef'></legend><bdo id='sWxef'><pre id='sWxef'><center id='sWxef'></center></pre></bdo></b><th id='sWxef'></th></span></q></dt></tr></i><div id='sWxef'><tfoot id='sWxef'></tfoot><dl id='sWxef'><fieldset id='sWxef'></fieldset></dl></div>
  3. <legend id='sWxef'><style id='sWxef'><dir id='sWxef'><q id='sWxef'></q></dir></style></legend>

      <tfoot id='sWxef'></tfoot>
    1. 使用 CMake 将多个静态库合二为一

      Combining several static libraries into one using CMake(使用 CMake 将多个静态库合二为一)

      <tfoot id='1Jmck'></tfoot>
      • <bdo id='1Jmck'></bdo><ul id='1Jmck'></ul>

            <small id='1Jmck'></small><noframes id='1Jmck'>

                <legend id='1Jmck'><style id='1Jmck'><dir id='1Jmck'><q id='1Jmck'></q></dir></style></legend>

                <i id='1Jmck'><tr id='1Jmck'><dt id='1Jmck'><q id='1Jmck'><span id='1Jmck'><b id='1Jmck'><form id='1Jmck'><ins id='1Jmck'></ins><ul id='1Jmck'></ul><sub id='1Jmck'></sub></form><legend id='1Jmck'></legend><bdo id='1Jmck'><pre id='1Jmck'><center id='1Jmck'></center></pre></bdo></b><th id='1Jmck'></th></span></q></dt></tr></i><div id='1Jmck'><tfoot id='1Jmck'></tfoot><dl id='1Jmck'><fieldset id='1Jmck'></fieldset></dl></div>

                  <tbody id='1Jmck'></tbody>
                本文介绍了使用 CMake 将多个静态库合二为一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个与 cmake 上的 描述 非常相似的问题邮件列表,我们有一个依赖于许多静态库的项目(全部从单个子模块中的源代码构建,每个子模块都有自己的 CMakeLists.txt 描述每个库的构建过程),我想将它们组合成一个静态库以供发布给消费者.我的库的依赖项可能会发生变化,我不希望这些变化给链下游的开发人员带来负担.简洁的解决方案是将所有库捆绑到一个库中.

                I have a very similar problem to one described on the cmake mailing list where we have a project dependent on many static libraries (all built from source in individual submodules, each with their own CMakeLists.txt describing the build process for each library) that I'd like to combine into a single static library for release to the consumers. The dependencies of my library are subject to change, and I do not want to burden developers further down the chain with those changes. The neat solution would be to bundle all of the libs into one single lib.

                有趣的是,在将目标设置为 mylib 并像这样使用它时,target_link_libraries 命令并没有结合所有的静态..

                Interestingly, the target_link_libraries command does not combine all of the statics when setting the target to mylib and using it like so . .

                target_link_libraries(mylib a b c d)
                

                然而,奇怪的是,如果我使 mylib 项目成为可执行项目的子模块,并且只链接到顶级可执行文件 CMAkeLists.txt 中的 mylib,库似乎是结合在一起的.IE.mylib 是 27 MB,而不是我将目标设置为仅构建 mylib 时的 3MB.

                However, bizarrely, if I make the mylib project a submodule of an executable project, and only link against mylib in the top level executable CMAkeLists.txt, the library does seem to be combined. I.e. mylib is 27 MB, instead of the 3MB when I set the target to only build mylib.

                有一些解决方案描述了将库解包为目标文件并重新组合(此处 和 此处),但这似乎非常当 CMake 看起来完全有能力自动合并库时,就像上面的例子中描述的那样笨拙.是否有我遗漏的魔法命令,或者推荐的制作发布库的优雅方式?

                There are solutions describing unpacking of the libs into object files and recombining (here, and here), but this seems remarkably clumsy when CMake seems perfectly capable of automatically merging the libs as described in the above example. It there a magic command I'm missing, or a recommended elegant way of making a release library?

                推荐答案

                给出我能想到的最简单的工作示例:2 个类,ab,其中a 依赖于 b ..

                Given the most simple working example I can think of: 2 classes, a and b, where a depends on b . .

                #ifndef A_H
                #define A_H
                
                class aclass
                {
                public:
                    int method(int x, int y);
                };
                
                #endif
                

                a.cpp

                #include "a.h"
                #include "b.h"
                
                int aclass::method(int x, int y) {
                    bclass b;
                    return x * b.method(x,y);
                }
                

                b.h

                #ifndef B_H
                #define B_H
                
                class bclass
                {
                public:
                    int method(int x, int y);
                };
                
                #endif
                

                b.cpp

                #include "b.h"
                
                int bclass::method(int x, int y) {
                    return x+y;
                }
                

                main.cpp

                #include "a.h"
                #include <iostream>
                
                int main()
                {
                    aclass a;
                    std::cout << a.method(3,4) << std::endl;
                
                    return 0;
                }
                

                可以将它们编译成单独的静态库,然后使用自定义目标组合静态库.

                It is possible to compile these into separate static libs, and then combine the static libs using a custom target.

                cmake_minimum_required(VERSION 2.8.7)
                
                add_library(b b.cpp b.h)
                add_library(a a.cpp a.h)
                add_executable(main main.cpp)
                
                set(C_LIB ${CMAKE_BINARY_DIR}/libcombi.a)
                
                add_custom_target(combined
                        COMMAND ar -x $<TARGET_FILE:a>
                        COMMAND ar -x $<TARGET_FILE:b>
                        COMMAND ar -qcs ${C_LIB} *.o
                        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
                        DEPENDS a b
                        )
                
                add_library(c STATIC IMPORTED GLOBAL)
                add_dependencies(c combined)
                
                set_target_properties(c
                        PROPERTIES
                        IMPORTED_LOCATION ${C_LIB}
                        )
                
                target_link_libraries(main c)
                

                使用 Apple 的 libtool 版本的自定义目标也可以正常工作...

                It also works just fine using Apple's libtool version of the custom target . . .

                add_custom_target(combined
                        COMMAND libtool -static -o ${C_LIB} $<TARGET_FILE:a> $<TARGET_FILE:b>
                        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
                        DEPENDS a b
                        )
                

                仍然接缝,好像应该有一个更整洁的方法..

                Still seams as though there should be a neater way . .

                这篇关于使用 CMake 将多个静态库合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                Delay Loading DLLs(延迟加载 DLL)
                Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                Loading a dll from a dll?(从 dll 加载 dll?)

                • <tfoot id='JN2rz'></tfoot>

                    <small id='JN2rz'></small><noframes id='JN2rz'>

                      <tbody id='JN2rz'></tbody>

                  1. <i id='JN2rz'><tr id='JN2rz'><dt id='JN2rz'><q id='JN2rz'><span id='JN2rz'><b id='JN2rz'><form id='JN2rz'><ins id='JN2rz'></ins><ul id='JN2rz'></ul><sub id='JN2rz'></sub></form><legend id='JN2rz'></legend><bdo id='JN2rz'><pre id='JN2rz'><center id='JN2rz'></center></pre></bdo></b><th id='JN2rz'></th></span></q></dt></tr></i><div id='JN2rz'><tfoot id='JN2rz'></tfoot><dl id='JN2rz'><fieldset id='JN2rz'></fieldset></dl></div>
                      <bdo id='JN2rz'></bdo><ul id='JN2rz'></ul>
                        1. <legend id='JN2rz'><style id='JN2rz'><dir id='JN2rz'><q id='JN2rz'></q></dir></style></legend>