strange linking error -- can not find shared library

This is a discussion on strange linking error -- can not find shared library within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am meeting with a strange linking error when linking with a shared library on Linux platform. I ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    strange linking error -- can not find shared library

    Hello everyone,


    I am meeting with a strange linking error when linking with a shared library on Linux platform. I use goo.c to generate libgoo.so, then write an application foo.c and link it with libgoo.so. The error occurs when linking with libgoo.so. The strange point is that, the LD_LIBRARY_PATH is not working, but -L option to gcc works -- I think they should be the same, but in my case below, it seems only -L works.

    Here are the source codes,

    goo.c

    Code:
    int goo_export()
    {
    	return 10;
    }
    goo.h

    Code:
    int goo_export();
    foo.c

    Code:
    #include "goo.h"
    
    int main()
    {
    	goo_export();
    }
    Error messages,

    [root@localhost shared_goo]# gcc -shared goo.c -o libgoo.so
    [root@localhost shared_goo]# export LD_LIBRARY_PATH=.
    [root@localhost shared_goo]# gcc -I. -lgoo foo.c
    /usr/bin/ld: cannot find -lgoo
    collect2: ld returned 1 exit status
    [root@localhost shared_goo]# gcc -I. -L. -lgoo foo.c


    thanks in advance,
    George

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    See if these work,even i learnt these only today:
    1. The object files that we use to create the .so file must be created with the -fPIC option. PIC stands for Position Independent Code.
    so, create the .o files using the -fPIC option first and then use the -shared option to create the .so file.

    2. echo the LD_LIBRARY_PATH and see it cantains the path to the .so file

    2. the LD_LIBRARY_PATH is dependent on the shell that you r using.
    1. if you are working with tcsh or csh :
    a.if the LD_LIBRARY_PATH is undeifned.

    you need to use
    Code:
       setenv LD_LIBRARY_PATH /complete path/
    b.if the LD_LIBRARY_PATH is deifned.

    Code:
          setenv    /complete path/  :$(LD_LIBRARY_PATH )
    2. if you are using the sh or bash

    then you will be using the "export" instead of setenv along the similar lines.
    Last edited by kris.c; 07-10-2006 at 10:43 PM.

  3. #3
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    i wish to add one more thing here:

    > The strange point is that, the LD_LIBRARY_PATH is not working, but -L option to gcc works -- I think they should be the same,

    There is a subtle difference in how the static and shared libraries are linked during compile time and runtime.

    For the shared lib, at compile time, the various symbols that have been used in the program will be matched with what is there in the .so file, u specify. But , the various object files that have been used to create the ".so" file will not be attached to the object code at this stage as against what is done in the static libraries..

    At run time, the dynamic loader , scans the various paths that have been specified in the LD_LIBRARY_PATH. If your lib file isnt there in this path you get an error. that's why u have to use setenv or export and set the path to your ".so" file depending on the shell that u sseem to be using.

    Atleast this is what i have understood. As i mentioned. I learnt this just yesterday..so, someone please correct me if i have gone wrong somewhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange linking error with operator overloading
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 07-03-2006, 07:32 AM
  2. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  3. Can't find library although I specify where it should look
    By Shogun in forum Linux Programming
    Replies: 4
    Last Post: 11-23-2004, 06:54 AM
  4. "Cant find main" linking fortran with c++
    By steve13 in forum C Programming
    Replies: 6
    Last Post: 06-26-2004, 10:28 PM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 07:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21