3

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

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Ramesh Kamath
  • 33
  • 1
  • 5
  • `gcc` will search in specific folders, see documentation for more details (you may start with `LIBRARY_PATH` environment variable and also compile with `-v` to see where it search). Alternatively you may give full path (anywhere it is) on `gcc` command line. – Adriano Repetti Nov 24 '15 at 11:15
  • How do you include this library? You have to include the header and link the library. – terence hill Nov 24 '15 at 11:33

3 Answers3

4

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?)

Community
  • 1
  • 1
terence hill
  • 3,354
  • 18
  • 31
3

You can copy that .a file (or better .so file) to a standard location such as /usr/lib.

If you compile from the command line, you also need to specify the library name (even if it's already in /usr/lib), the same way when you specify standard library like -lpthread.

You can also specify the library path and library name in makefile

Community
  • 1
  • 1
artm
  • 17,291
  • 6
  • 38
  • 54
1

As Adriano wrote, you can include a library even if it is not in the same directory as your c file. However, you have to specify where to look for for the libraries you use.

see here: https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html

Hope I helped,

Shay Gold
  • 403
  • 4
  • 14