Skip to content

Automatically merge target libraries ​

After 2.5.8, we can enable automatic merging of all dependent static libraries by setting the build.merge_archive strategy, for example:

EXPLORER
src
add.c
mul.c
sub.c
xmake.lua
Lua xmake.lua
123456789101112131415
add_rules("mode.debug", "mode.release")

target("add")
    set_kind("static")
    add_files("src/add.c")

target("sub")
    set_kind("static")
    add_files("src/sub.c")

target("mul")
    set_kind("static")
    add_deps("add", "sub")
    add_files("src/mul.c")
    set_policy("build.merge_archive", true)

The mul static library automatically merges the add and sub static libraries to generate a complete libmul.a library containing add/sub code.

This merge is relatively stable and complete, supports ar and msvc/lib.exe, also supports the merge of static libraries generated by the cross-compilation tool chain, and also supports static libraries with the same name obj file.

Merge specified static library files ​

If the automatic merge does not meet the requirements, we can also actively call the utils.archive.merge_archive module to merge the specified static library list in the after_link stage.

lua
target("test")
    after_link(function (target)
        import("utils.archive.merge_staticlib")
        merge_staticlib(target, "libout.a", {"libfoo.a", "libbar.a"})
    end)

Use add_files to merge static libraries ​

In fact, our previous version already supports merging static libraries through add_files("*.a").

lua
target("test")
    set_kind("binary")
    add_files("*.a")
    add_files("*.c")

Related issues: #1638