Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a GCC compiled static library contain?

My application links against libsamplerate.a. I am doing this to make distributing the final binary easier.

I am worried that perhaps the code inside the .a file depends on some other libraries I also will need to distribute.

But if it doesn't I am worried I am bloating up my application too much by including multiple copies of eg. libc.

What exactly will be inside libsamplerate.a? Just libsamperate's bytecode? Or more?

like image 327
mxcl Avatar asked Oct 31 '08 18:10

mxcl


People also ask

What does static library contain?

In the C programming language, a static library is a compiled object file containing all symbols required by the main program to operate (functions, variables etc.) as opposed to having to pull in separate entities. Static libraries aren't loaded by the compiler at run-time; only the executable file need be loaded.

What is static in gcc?

The -static option links a program statically, in other words it does not require a dependency on dynamic libraries at runtime in order to run. To achieve static linking requires that the archive ( . a ) versions of your libraries exist on the system. so /usr/lib/libc.

What is the format of a static library?

Static libraries are simply a collection of ordinary object files; conventionally, static libraries end with the ``. a'' suffix. This collection is created using the ar (archiver) program. Static libraries aren't used as often as they once were, because of the advantages of shared libraries (described below).

Does static library include dependencies?

So what is a Static library?? When linked like this the library is called a static library, because the library will remain unchanged unless the program is recompiled. This is the most straight forward way of using a library as the final result is a simple executable with no dependencies.


2 Answers

A .a file is basically just a bundle of .o files. You can demonstrate this using the ar tool.

For instance, to display the contents of your library:

ar -t libsamplerate.a

To create a .a file from scratch:

ar -r tim.a *.txt
like image 182
Tim Robinson Avatar answered Oct 03 '22 07:10

Tim Robinson


A static library is just a collection of object files. When you compile a program against a static library, the object code for the functions used by your program is copied from the library into your executable. Linking against a static library will not cause any functions outside that library to be included in your code.

like image 35
Robert Gamble Avatar answered Oct 03 '22 06:10

Robert Gamble