[CMake] Combining two static libraries into one

vinay kumar Kotegowder vinay.kotegowder at gmail.com
Wed Jul 3 09:23:33 EDT 2019


 I want to build lib2 static array with pre-built lib1 static array.
 Does this work?

 add_library(lib1 STATIC lib1.c)
 add_library(lib2 STATIC lib2.c)
 target_link_libraries(lib2 INTERFACE lib1)

 Also, Kyle said linking against lib2 will link against both lib1 and
 lib2. Is there a static way of checking the relation between lib1 and
 lib2 ? Separately and after using INTERFACE option with lib1 and lib2

 Regards,
 Vinay

On Tue, Jul 2, 2019 at 8:48 PM Kyle Edwards <kyle.edwards at kitware.com> wrote:
>
> On Tue, 2019-07-02 at 20:44 +0530, vinay kumar Kotegowder wrote:
> > Hi Everyone,
> >
> > I have a requirement on combining two static library into one.
> > Can anyone tell me how can I achieve this?
> >
> > Regards,
> > Vinay
>
> If you just want to make a library such that linking against that
> library links against your two static libraries, you could use an
> interface library. Example:
>
> add_library(lib1 STATIC lib1.c)
> add_library(lib2 STATIC lib2.c)
> add_library(lib1_2 INTERFACE)
> target_link_libraries(lib1_2 INTERFACE lib1 lib2)
>
> Note that with this strategy, lib1 and lib2 will still exist as
> separate files, but linking against lib1_2 will link against both of
> these libraries.
>
> Kyle


More information about the CMake mailing list