LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-12-2007, 09:13 PM   #1
pbhat
Member
 
Registered: Apr 2004
Location: India
Distribution: Opensuse 11.3
Posts: 34

Rep: Reputation: 15
how to call shared library from within a C program


Hello List,

I need to call a C program from web(cgi-bin).This program depends upon an SDK in the form of binary shared library.But on the target machine which is hosted web-site,the hosting provoder is not willing to place them there.Neither is he willing to include the library path in ld.so.conf.

This is a quick question.I want to know if it is possible for a C program to call it's shared library by itslef into memory?

OR Can a shared library be compiled into a binary?

As I have not done this before,I am asking the question.Any help will be great.

Thanks in advance.
 
Old 11-12-2007, 09:53 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
If you link with the -static option when you build your program, everything which is used will be copied from shared libraries into your executable. Of course the executable will be larger than if it is shared, but it should run regardless of the available shared libraries on the target system.
 
Old 11-14-2007, 04:19 AM   #3
pbhat
Member
 
Registered: Apr 2004
Location: India
Distribution: Opensuse 11.3
Posts: 34

Original Poster
Rep: Reputation: 15
Hello matthewg42,

I tried your suggestion.gcc complains it cannot find the library when I use the option '-static'.The files are *so files.I tried by giving full name of the files.If I omit it without changing any other option,it compiles fine. So, my syntax is right.Am I missing something here?

Thank You.
 
Old 11-18-2007, 11:58 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
What is the exact command you are using to build with?
 
Old 11-18-2007, 09:30 PM   #5
pbhat
Member
 
Registered: Apr 2004
Location: India
Distribution: Opensuse 11.3
Posts: 34

Original Poster
Rep: Reputation: 15
Hi,

My command is,

gcc -o 'somename.cgi' -L'dir_of_shared_lib' \
-l'NImage_where_lib_is_libNImage.so' -l'simialr' -l'simialr'\
-static file.c

That results in error 'file not found'.

But if I omit '-static' option that compiles.

I do not have source to shared lib files which comec from a proprietary SDK.

thanks

P.Bhat
 
Old 11-18-2007, 10:20 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You should not include the .so when using the -l option, e.g. to use libbananas.a / libbananas.so, you would use the -l option like this:
Code:
gcc -lbananas ...
 
Old 11-18-2007, 11:16 PM   #7
pbhat
Member
 
Registered: Apr 2004
Location: India
Distribution: Opensuse 11.3
Posts: 34

Original Poster
Rep: Reputation: 15
Exactly.I am calling that way only.I was trying to show the involved library name.That's all.

PBhat
 
Old 11-19-2007, 12:23 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Please paste the actual command you are using, not some representation of it...
 
Old 11-19-2007, 02:38 AM   #9
pbhat
Member
 
Registered: Apr 2004
Location: India
Distribution: Opensuse 11.3
Posts: 34

Original Poster
Rep: Reputation: 15
Command with -static option

pbhat@pb:/media/disk/test/Code$ gcc-2.95 -o verify.cgi -L/media/disk/test/libs -lNCore -lNImages -lVLExtractor -lVLMatcher -lVLook verify.c verifying.c -I/media/disk/test/includes/ -static
/usr/bin/ld: cannot find -lNCore
collect2: ld returned 1 exit status

Command without -static option

pbhat@pb:/media/disk/test/Code$ gcc-2.95 -o verify1.cgi -L/media/disk/test/libs -lNCore -lNImages -lVLExtractor -lVLMatcher -lVLook verify.c verifying.c -I/media/disk/test/includes/
pbhat@pb:/media/disk/test/Code$


P.Bhat
 
Old 11-19-2007, 03:44 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Find libNCore.a and add that as an object file. e.g. if the libNCore.a file is found in /usr/lib, do it like this:
Code:
gcc-2.95 -o verify.cgi -L/media/disk/test/libs -lNImages -lVLExtractor -lVLMatcher -lVLook verify.c verifying.c -I/media/disk/test/includes/ -static /usr/lib/libNCore.a
 
Old 11-19-2007, 04:25 AM   #11
pbhat
Member
 
Registered: Apr 2004
Location: India
Distribution: Opensuse 11.3
Posts: 34

Original Poster
Rep: Reputation: 15
Hi,

I do not have *.a library files which is not provided by the SDK.And I have no privilege to place these *.so files in standard library path on the target system which is at the root of my difficulty.

But this is solved by -wl,-R/path option to the gcc

P Bhat
 
Old 11-19-2007, 09:49 AM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Please can you post the solution command?
 
Old 11-19-2007, 11:09 AM   #13
pbhat
Member
 
Registered: Apr 2004
Location: India
Distribution: Opensuse 11.3
Posts: 34

Original Poster
Rep: Reputation: 15
Here is the code.My previous example lowercased 'w' in '-Wl,-R/path'.Uppercase is right.

[HTML]gcc-2.95 -o verify1.cgi -L/media/disk/test/libs/ -lNCore -lNImages -lVLExtractor -lVLMatcher -lVLook verify.c verifying.c -I/media/disk/test/includes/ -Wl,-R/hshere/local/home/user/*.co.in/faceidentity/lib/[/HTML]

P Bhat

Last edited by pbhat; 11-19-2007 at 11:12 AM.
 
  


Reply

Tags
compile, gcc, library, link, static



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
list library function of a shared library .so powah Linux - General 7 10-25-2011 04:47 AM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
Call back method for shared library arunka Programming 2 02-25-2006 11:43 AM
JAVA JNI program to access shared library sabliny Programming 3 11-17-2005 05:13 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:55 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
Open Source Consulting | Domain Registration