LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 01-15-2009, 06:37 AM   #1
sreeharsha.t
Member
 
Registered: Jan 2009
Location: Mumbai, India
Distribution: Fedora 9, 10,11, RHEL 5
Posts: 67

Rep: Reputation: 18
Manually compiling shared libraries


Hi,

I am trying to compile a simple shared library on my linux box. I m running RHEL 5.

This is the code for the lirary file
//-----------------libhello.c---------
#include <stdio.h>

void sayhello(){
printf("library saying hello!! \n");
}

this is the header file
//--------------libhello.h------

//declaring the functions!!

void sayhello();

I have compiled the header source file as
gcc -fPIC -c -o libhello.o libhello.c

and then, to make it a shared library,
gcc -shared -Wl,-soname,libhello.so.1 -o libhello.so.1.0.0 libhello.o

my PWD: /home/harsha/lib

then I used ldconfig as
/sbin/ldconfig -v -n . (output shown below)
.:
libhello.so.1 -> libhello.so.1.0.0

and I have set the LD_LIBRARY_PATH env var as
export LD_LIBRARY_PATH=/usr/harsha/lib

now I wrote a simple program to used the above created shared library
//--------------hello.c-----------------
#include "libhello.h"

int main(void)
{
sayhello();
}

and compiled as
gcc -c -o hello.o hello.c

now when I try to link the above object file and the shared library I am facing this error..
gcc -o dynamic_out hello.o -L. -lhello
/usr/bin/ld: cannot find -lhello
collect2: ld returned 1 exit status

the soname link has been created by the ldconfig and somehow this error is showing up..
 
Old 01-15-2009, 08:27 AM   #2
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2014.08, Slack14.1 DebWheezy, +50+ other Linux OS, for test only.
Posts: 14,286

Rep: Reputation: 2664Reputation: 2664Reputation: 2664Reputation: 2664Reputation: 2664Reputation: 2664Reputation: 2664Reputation: 2664Reputation: 2664Reputation: 2664Reputation: 2664
Welcome to Linux Questions.

Very simple :
'ln -s libhello.so.1 libhello.so'

Reason : If you have e.g.
libhello.so.1.0.0, libhello.so.2.0.0, libhello.so.3.0.0
it will be your decision, which one to compile against.

So the linker ( ld ) will use "lib.so" only.
....
 
Old 01-16-2009, 12:04 AM   #3
sreeharsha.t
Member
 
Registered: Jan 2009
Location: Mumbai, India
Distribution: Fedora 9, 10,11, RHEL 5
Posts: 67

Original Poster
Rep: Reputation: 18
It worked!! :)

Thanks pal..

Creating the symbolic link worked.. I was little confused before understanding how ld determines which library version to use when multiple versions of the same library exits.. but now, your post clarifies a lot!

Kudos.
 
  


Reply

Tags
compilation, library, linking, shared


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gxine: error while loading shared libraries: libmozjs.so: cannot open shared object.. empcrono Slackware 10 11-10-2008 08:33 PM
error while loading shared libraries: libstdc++.so.5: cannot open shared object file PaulyWally Debian 2 10-18-2008 06:59 PM
Cross compiling to Windows binary, including Linux shared libraries Siiiiiii Programming 2 03-09-2007 01:36 AM
error while loading shared libraries: libdb-4.1.so: cannot open shared object file putquery8581 Linux - Software 1 10-01-2004 08:03 AM
compiling with shared and static libraries jinksys Programming 1 11-22-2003 05:54 AM


All times are GMT -5. The time now is 03:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration