---
url: /examples/cpp/merge-static-libraries.md
---
## 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:

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](https://github.com/xmake-io/xmake/issues/1638)
