Below are the steps of how I generate the executable file using shared library.
-
I have three files:
File libhello.c
/* hello.c - demonstrate library use. */ #include <stdio.h> void hello(void) { printf("Hello, library world./n"); }File libhello.h
/* hello.h - demonstrate library use. */ void hello(void);File main.c
/* main.c -- demonstrate direct use of the "hello" routine */ #include "hello.h" int main(void) { hello(); return 0; } -
I use the commands below to generate the shared library.
gcc -g -Wall -fPIC -c hello.c -o hello.o gcc -shared -W,soname,-libhello.so.0 -o libhello.so.0.0.0 hello.o -
Finally, I add the library path to the
LD_LIBRARY_PATHvariable and try to create the executable file using the shared library.export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ln -s libhello.so.0.0.0 libhello.so.0 gcc -g -Wall -c main.c -o main.o -I. gcc -o main main.o -lhello -L.
However, at the last step, there is one error: can’t find -lhello. So, where am I wrong?
Thanks.
gcclooks forlibhello.sowhen linking a new program.libhello.so.0is used when the dynamic dependencies of an already linked program are searched.In other terms:
gcc -o main main.o -lhello -L.looks forlibhello.so, and./mainlooks forlibhello.so.0. This allows to have multiple versions of a library available for legacy programs while precisely identifying the library that matches the installed headers.A symlink
libhello.so->libhello.so.0.0.0should do the trick.