Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to create/merge multiple static library into single static library

7,430 views
Skip to first unread message

Vipin

unread,
Jun 2, 2009, 1:47:01 PM6/2/09
to
Hi ,

I am working on UNIX platform. I have three lib files and one object
file of which I want to make single Library. structure is as below
PWD/libone/libone.a
PWD/libone/libtwo.a
PWD/libmin/libmin.a
PWD/main.o

I want to compile/combine these all into one SINGLE STATIC LIBRARY
with the name libfinal.a

I tried the following
$ar -rc libfinal.a ./libone/libone.a ./libtwo/libtwo.a ./libmin/
libmin.a main.o

and it generated the libfinal.a but when I link it with main function
(main2.c) using
$gcc -o run main2.c -lfinal -L./
I get the undefined reference error for the functions which were
actually part of previous libraries

I tried fixing it with
$ranlib libfinal.a
assuming that may be just index was missing but it didn't help and it
won't because
$nm libfinal.a
gives the error that unknown format of libone.a libtwo.a and libmin.a

Is there any other way on UNIX based platforms to generate SINGLE
STATIC LIBRARY out of other multiple static LIBRARIES and object
files.

http://www.mail-archive.com/cm...@cmake.org/msg07427.html
has created a panic in me.

Thanks for uregent help
vipin


Nate Eldredge

unread,
Jun 2, 2009, 2:03:24 PM6/2/09
to
Vipin <sh.v...@gmail.com> writes:

> Is there any other way on UNIX based platforms to generate SINGLE
> STATIC LIBRARY out of other multiple static LIBRARIES and object
> files.

Extract the object files from the existing libraries with `ar x'. Then
create the new library out of all the extracted object files and any new
ones using `ar rcs'. (If your version of `ar' does not know the `s'
option, then skip it and run `ranlib' afterwards.)

Vipin

unread,
Jun 3, 2009, 8:13:49 AM6/3/09
to
On Jun 2, 11:03 pm, Nate Eldredge <n...@vulcan.lan> wrote:

Thanks. It worked But I have something extra for you guys.

When I was making libfinal.a from {libone.a libtwo.a libmin.a main.o }
later on I observed that I had to use /usr/lib/libpthread.a also for
linking.


Following the same method, i extracted all the lib*.a (including /usr/
lib/libpthread.a) and then did
$ar x libone.a libtwo.a libmin.a libpthrea.a
$ar rcs libfinal.a *.o
$gcc -o run main2.o -lfinal

but it gave an undefined reference error for
<__pthread_reset_main_thread, _errno, _h_errno > to name a few


ANOTHER ATTEMPT
-------------------
i tried the following

$ar x libone.a libtwo.a libmin.a
$ar rcs libfinal.a *.o
$gcc -o run main2.o -lfinal -lpthread

basically linking -pthread.a later on and the program works now.


Not sure what's wrong happening when I am trying to extract and
earchive libpthread.a.
Looking for your suggestions.???


vipin


rfr...@gmail.com

unread,
Jun 3, 2009, 7:58:28 PM6/3/09
to
On Jun 2, 10:47 am, Vipin <sh.vi...@gmail.com> wrote:
>
> Is there any other way on UNIX based platforms to generate SINGLE
> STATIC LIBRARY out of other multiple static LIBRARIES and object
> files.
>
> http://www.mail-archive.com/cm...@cmake.org/msg07427.html
> has created a panic in me.
>
> Thanks for uregent help
> vipin

Unfortunately ar does not support this. You will have to extract the
object files and then combine then into a static library.

ar -xv libone/libone.a
ar -xv libone/libtwo.a
ar -xv libmin/libmin.a
ar -rc libfinal.a *.o

--
rahul

only...@gmail.com

unread,
May 28, 2019, 2:46:53 AM5/28/19
to
```
ar -x libx264.a
mkdir sub && cd sub
ar -m ../libx264.a `ar -t ../libx264.a |sort|uniq|grep "\.o"`
ar -x ../libx264.a
```
now you have two version of "macroblock-10.a"
0 new messages