# Combining multiple static libraries onto one. How to retrieve list of static libraries from target?

**URL:** https://discourse.cmake.org/t/combining-multiple-static-libraries-onto-one-how-to-retrieve-list-of-static-libraries-from-target/5302
**Category:** Code
**Created:** [March 26, 2022, 3:44pm UTC](https://discourse.cmake.org/t/combining-multiple-static-libraries-onto-one-how-to-retrieve-list-of-static-libraries-from-target/5302 "2022-03-26T15:44:05Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![hgkjshegfskef](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hgkjshegfskef/32/2301_2.png) [@hgkjshegfskef](https://discourse.cmake.org/u/hgkjshegfskef)
#### Post date: [March 26, 2022, 3:44pm UTC](https://discourse.cmake.org/t/combining-multiple-static-libraries-onto-one-how-to-retrieve-list-of-static-libraries-from-target/5302/1 "2022-03-26T15:44:05Z")

</div>

Platform: Linux, Ninja.

Goal: combine many static libraries into one static library, i.e. run something like `ar rcs hugelib.a lib1.a lib2.a [...]`. Why? For the user to use C/C++ source files in script mode, and so that user only needs to compile the file and link it to one single library that is shipped with their environment.

Problem: cannot find a way to retrieve all link dependencies of a target and automate this procedure. For example, the link line of the user-written C++ file:

```auto
[2/2] : && /usr/bin/c++ -Wall -Wextra -g
src/cxx_client/CMakeFiles/sbal_testcase_01.dir/ __/__ /tests/sbal/testcase_01/SBAL.cc.o
-o bin/sbal_testcase_01 lib/libsbal_cxx_client.a lib/libsbal_protocols.a
/usr/local/lib/libgrpc++.a /usr/local/lib/libgrpc.a /usr/local/lib/libz.a
/usr/local/lib/libcares.a -lnsl /usr/local/lib/libaddress_sorting.a
/usr/local/lib/libre2.a /usr/local/lib/libabsl_raw_hash_set.a
/usr/local/lib/libabsl_hashtablez_sampler.a /usr/local/lib/libabsl_hash.a
/usr/local/lib/libabsl_city.a /usr/local/lib/libabsl_low_level_hash.a
/usr/local/lib/libabsl_statusor.a /usr/local/lib/libabsl_bad_variant_access.a
/usr/local/lib/libgpr.a /usr/local/lib/libupb.a -ldl -lm -lrt
/usr/local/lib/libabsl_status.a /usr/local/lib/libabsl_random_distributions.a
/usr/local/lib/libabsl_random_seed_sequences.a
/usr/local/lib/libabsl_random_internal_pool_urbg.a
/usr/local/lib/libabsl_random_internal_randen.a
/usr/local/lib/libabsl_random_internal_randen_hwaes.a
/usr/local/lib/libabsl_random_internal_randen_hwaes_impl.a
/usr/local/lib/libabsl_random_internal_randen_slow.a
/usr/local/lib/libabsl_random_internal_platform.a
/usr/local/lib/libabsl_random_internal_seed_material.a
/usr/local/lib/libabsl_random_seed_gen_exception.a
/usr/local/lib/libabsl_cord.a /usr/local/lib/libabsl_bad_optional_access.a
/usr/local/lib/libabsl_cordz_info.a /usr/local/lib/libabsl_cord_internal.a
/usr/local/lib/libabsl_cordz_functions.a
/usr/local/lib/libabsl_exponential_biased.a
/usr/local/lib/libabsl_cordz_handle.a
/usr/local/lib/libabsl_str_format_internal.a
/usr/local/lib/libabsl_synchronization.a /usr/local/lib/libabsl_stacktrace.a
/usr/local/lib/libabsl_symbolize.a /usr/local/lib/libabsl_debugging_internal.a
/usr/local/lib/libabsl_demangle_internal.a
/usr/local/lib/libabsl_graphcycles_internal.a
/usr/local/lib/libabsl_malloc_internal.a /usr/local/lib/libabsl_time.a
/usr/local/lib/libabsl_strings.a /usr/local/lib/libabsl_throw_delegate.a
/usr/local/lib/libabsl_int128.a /usr/local/lib/libabsl_strings_internal.a
/usr/local/lib/libabsl_base.a /usr/local/lib/libabsl_spinlock_wait.a -lrt
/usr/local/lib/libabsl_raw_logging_internal.a
/usr/local/lib/libabsl_log_severity.a /usr/local/lib/libabsl_civil_time.a
/usr/local/lib/libabsl_time_zone.a /usr/local/lib/libssl.a
/usr/local/lib/libcrypto.a -lpthread /usr/local/lib/libprotobuf.a
/home/hgkjshegfskef/.local/soft/trace/build/installed/lib/libtrace.a && :

```

I need a list of paths to these static libraries, to pass them to `ar`, to create a single static library. The action of creating such archive is expected to be modelled with `add_custom_command`. I have tried using `$<TARGET_PROPERTY:user_file_target,LINK_LIBRARIES>`, but it only lists the direct dependencies, and in CMake target names instead of paths to archives, while I need the full dependency list, and in paths to archives.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 29, 2022, 1:24pm UTC](https://discourse.cmake.org/t/combining-multiple-static-libraries-onto-one-how-to-retrieve-list-of-static-libraries-from-target/5302/2 "2022-03-29T13:24:39Z")

</div>

This thread may be of interest:

> [@does POST\_BUILD work with add\_custom\_target and Visual Studio?](https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290):
>
> I build a static library and then want to add two more static libs to it after it is built. The documentation for add\_custom\_command/“BUILD EVENTS” says The command becomes part of the target and will only execute when the target itself is built. If the target is already built, the command will not execute. I have a command like add\_custom\_command( TARGET something POST\_BUILD COMMAND lib $\<TARGET\_FILE:something\> $\<TARGET\_FILE:extraLib\> $\<TARGET\_FILE:anotherLib\> ) However, in pra…
