R. Brock Lynn wrote:
> 
> I've recently acquired a copy of the new "Linux Application Development" by
> Michael K. Johnson and Erik W. Troan.
> 
> In it, it describes how to create and use shared libraries. But when I follow
> the instructions. It doesn't work.
> 
> Here is the example:
> 
> `libhello.c'
> -------------------------------------------------------------------------------
> #include <stdio.h>
> void print_hello(void){
>   printf("Hello, shared library.\n");
> }
> -------------------------------------------------------------------------------
> 
> `libhello.h' (this file was not included, but below is a best guess)
> -------------------------------------------------------------------------------
> void print_hello(void);
> -------------------------------------------------------------------------------
> 
> `usehello.c'
> -------------------------------------------------------------------------------
> #include "libhello.h"
> int main(void){
>   print_hello();
>   return 0;
> }
> -------------------------------------------------------------------------------
> 
> Compile and Link instructions:
> 
> gcc -fPIC -Wall -g -c libhello.c
> 
> gcc -g -shared -Wl,-soname,libhello.so.0 -o libhello.so.0.0 libhello.o -lc
> 
> ln -sf libhello.so.0.0 libhello.so.0
> 
> ln -sf libhello.so.0 libhello.so
> 
> gcc -Wall -g -c usehello.c -o usehello.o
> 
> gcc -g -o usehello usehello.o -L. -lhello
> 
> Then run with:
> 
> LD_LIBRARY_PATH='pwd' ./usehello
> 
> But I get this error:
> 
> ./usehello: can't load library 'libhello.so.0'
> 
> Any suggestions?

yes! I changed those compile instructions a little, and then it works.
(my gcc does not recognize a 'soname' option. does yours?)

try this: (it works)

gcc -fPIC -c libhello.c                      // libhello.o
gcc -shared libhello.o -o libhello.so        // libhello.so      
gcc -c usehello.c                            // usehello.o
gcc usehello.o -o usehello -L . -lhello      // dynamically linked
usehello
setenv LD_LIBRARY_PATH `pwd`                 // (I use tcsh)

!!! note the grave quotes: `` not '' 
(you could also type setenv LD_LIBRARY_PATH /the/correct/path instead )
check by doing echo $LD_LIBRARY_PATH
it should say something like LD_LIBRARY_PATH=/where/your/libhello.so/is
otherwise libhello.so cannot be found.

good luck!
        Christoph

Reply via email to