Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link to a static library in C?

People also ask

How are libraries linked in C?

Shared libraries (also called dynamic libraries) are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries.

What is static linking in C?

Static linking is the result of the linker copying all library routines used in the program into the executable image. This may require more disk space and memory than dynamic linking, but is both faster and more portable, since it does not require the presence of the library on the system where it is run.


cc -o yourprog yourprog.c -lstatic

or

cc -o yourprog yourprog.c libstatic.a

You should #include "libstatic.h", i.e. use the appropriate header file in your code (that's why your code doesn't compile) and include the path to your libstatic.a in the linker options as one of your input libraries.

This webpage has some examples on linking to a static library, e.g.

gcc -I. -o jvct jvct.c libjvc.a

I had to set the library path in my makefile. For this case you could use:

gcc -o myapp main.c -L. -lstatic

gcc -I. -o jvct jvct.c libjvc.a

To link purely statically, use -static

cc -static yourprogram.c libstatic.a