Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a custom library to c programs

I am using Ubuntu Linux I have made a custom static library Mylib.a, I can include it to only those c files which are in the same directory as the static library.

I want to make it a general library so that I can include the library file to any c file I want irrespective of its location

like image 740
Ramesh Kamath Avatar asked Sep 13 '25 15:09

Ramesh Kamath


1 Answers

To use a static library you have to include the header in the .c files that use the library and then link the library. If the name of the library is libstatic.a then:

gcc -o yourprog yourprog.c -lstatic

If the library is not in the same directory use the -L option to specify the path:

gcc -o yourprog yourprog.c -L/path-to-lib -lstatic

(see also this post: How to link to a static library in C?)

like image 54
terence hill Avatar answered Sep 16 '25 05:09

terence hill