2

How do I create a custom library in GNU? What I mean is:

When we use #include < stdio.h> and printf

we can compile it with gcc main.c

Now I create my custom headers and .a/.so library files, I know I can set the environment variable C_INCLUDE_PATH and include my header files with #include<> instead of #include"". However, I still have to compile it with

gcc main.c -o program -L/whatever/ -lwahtever

(with set environment variable if using .so)

Is it possible to make it behave like #include< stdio.h> where I don't need to include the paths with corresponding command line arguments?

Thenewstockton
  • 443
  • 6
  • 18

1 Answers1

0

You actually don't need -L/whatever/, just -lwhatever. The first option supplies the path to your library, but you have already taken care of that with the #include and modifying C_INCLUDE_PATH. The second option tells the linker which library to link your executable with. An example of this is when using functions from the C math library, you #include <math.h>, but to compile, you still need the linker option -lmath. So to answer your question, no. You can remove the first option, but you must leave the second.

  • It doesn't work.... If I use gcc main.c -o program lwhatever. the linker complains about not finding lwhatever. Am I missing something? – Thenewstockton Jul 29 '16 at 03:55
  • Did you include the dash in front? It should be `gcc main.c -o program -lwhatever`. Also the naming conventions of GNU libraries means your library should be `libwhatever.a` or `libwhatever.so`. Apologies. – Daniel Margosian Jul 29 '16 at 04:09
  • No... it doesn't work... It seems like -L/whatever/ is required... Maybe some environment other than C_INCLUDE_PATH needs to be set? – Thenewstockton Jul 29 '16 at 04:32
  • I found it. You have to set the environment variable LIBRARY_PATH in order to remove -L/whatever/ . I still wonder if you can remove '-l' since libraries like stdio.h don't require '-l' as command line arguments – Thenewstockton Jul 29 '16 at 04:35
  • Oh so that's what it was. You can't remove the second flag. When you use functions in stdio.h, you are using the libc library, which is linked with your executable by default. Further reading here: http://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c – Daniel Margosian Jul 29 '16 at 12:41