The answer using gcc:
Header files are generally used to hold the declaration of the function. The definition of the function is present in a separate file (the .c file) to link this declaration and definition we create shared objects (I think there is another method which does not use shared objects but I do not know how to do that.) For any file foo.c that has the declarations in foo.h:
$ ls
foo.c foo.h
$ gcc -Wall -ansi -pedantic -c -o foo.o foo.c -I.
$ ls
foo.c foo.h foo.o
$ ld -G -o libfoo.so foo.c
$ ls
foo.c foo.h foo.o libfoo.so
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/foodir
To use any function that is present in this .so file in your system (Assuming that the .so is present in the directory /usr/lib/foodir/).
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/foodir
$ gcc -o myprog myprog.c -Wall -shared -static -lfoo -L/usr/lib/foodir/
The path after -L is the path to the library files and multiple -L are allowed. It can be substituted by relevant paths.
The export statement need not be repeated if this is in the same session or a child session.
Hope this helps.
Rgds,
Karthick S.
Monday, September 06, 2004
GCC - Creating shared objects and using them.
Subscribe to:
Posts (Atom)