Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

link .a and .o files in GCC

I have two precompiled library: X.a and Y.a and a test.cpp (without main function) source code use these two libraries.

I compiled the C++ using:

g++ -c test.cpp

and I got 'test.o'.

Now how can I link these three together to generate a .a file because test.cpp use some function in X.a and Y.a and other GCC libraries?

BTW, I am doing these under Windows using MinGW. Can I rename this .a file to .lib and use this .lib in VC?

Thanks!

like image 709
Yin Zhu Avatar asked Feb 26 '23 22:02

Yin Zhu


2 Answers

Now how can I link these three together to generate a .a file because test.cpp use some function in X.a and Y.a and other GCC libraries?

.a is nothing more then ar archive containg all object files (.o files)

Can I rename this .a file to .lib and use this .lib in VC?

Yes, but it requires little trick to work. See: http://opensees.berkeley.edu/community/viewtopic.php?t=2267

like image 69
vartec Avatar answered Mar 05 '23 16:03

vartec


In order to add your object file to the static library you have to use the following command:

ar rcs X.a test.o

But if you are provided with X.a and Y.a I suppose you are not the author of X and Y, therefore I'm not sure you really want to join them!

You may decide to link every object file into a single executable instead, but you need the main function for it!

EDIT: Also I suggest you to read this.

like image 30
Dacav Avatar answered Mar 05 '23 15:03

Dacav