it is awkward, but until now i always copy the *.h and the *.c files to my projekts location. this is a mess and i want to change it!
i want to build my own c library and have a few questions about it.
where should i locate the *.h files?
should i copy them in the global /usr/include/ folder or should i create my own folder in $HOME (or anywhere else)?
where should i locate the *.a files and the *.o files and where the *.c files.
i am using debian and gcc. my c projects are in $HOME/dev/c/.
i would keep my lib-sources in $HOME/dev/c/lib (if this is the way you could recommend) and copy the *.o, *.a and *.h files to the location i am asking for.
how would you introduce the lib-location to the compiler? should i add it to the $PATH or should i introduce it in the makefiles of my projekt (like -L PATH/TO/LIBRARY -l LIBRARY).
do you have anny further tips and tricks for building a own library?
I would recommend putting the files somewhere in your $HOME
directory. Let's say you've created a library called linluk
and you want to keep that library under $HOME/dev/c/linluk
. That would be your project root.
You'll want a sensible directory structure. My suggestion is to have a lib
directory containing your library and an include
directory with the header files.
$PROJECT_ROOT/
lib/
liblinluk.so
include/
linluk.h
src/
linluk.c
Makefile
Compiling: When you want to use this library in another project, you'd then add -I$PROJECT_ROOT/include
to the compile line so that you could write #include <linluk.h>
in the source files.
Linking: You would add -L$PROJECT_ROOT/lib -llinluk
to the linker command line to pull in the compiled library at link time. Make sure your .so
file has that lib
prefix: it should be liblinluk.so
, not linluk.so
.
A src
directory and Makefile
are optional. The source code wouldn't be needed by users of the library, though it might be helpful to have it there in case someone wants to remake the .so
file.