LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-14-2012, 12:51 AM   #1
pradiptart
Member
 
Registered: Sep 2007
Posts: 102

Rep: Reputation: 12
Not able to link shared library.


Hi all,

I am building my application.I have 10 cpp files and I made one make file to compile those .My problem is that my application is dependent upon some library that i have to put in some directory.

for example i am storing my library as follow
here i am storing 2 libraries
1st in /opt/my_lib_root
2nd in /opt/my_lib_root/my_lib/

but when my application running it showing

error while loading shared libraries: libproduct.so.1: cannot open shared object file: No such file or directory
as I am depending upon this library and it is the 1st library.

I know it is not linked properly ,when i am using

export LD_LIBRARY_PATH=/opt/my_lib_root/my_lib/:/opt/my_lib_root/
it is working.

but i don't want to do in mannualy how to mentioned this one during compilation of the project.

During compilation how to do this or is there any solution for these linking error.

Thanks

Last edited by pradiptart; 06-14-2012 at 12:52 AM.
 
Old 06-14-2012, 03:33 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
this is the normal behavior, it is not an error. A shared object (*.so file) usually located in /usr/lib or /lib and in your case the file is located in another dir, that's why you need to use LD_LIBRARY_PATH. Another solution can be to hardcode the path of that lib into your app. This can be made by linking this lib with full path to your app.






_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
 
Old 06-14-2012, 04:36 AM   #3
pradiptart
Member
 
Registered: Sep 2007
Posts: 102

Original Poster
Rep: Reputation: 12
Hi pan64 ,
Thanks for reply.


Quote:
Another solution can be to hardcode the path of that lib into your app. This can be made by linking this lib with full path to your app.
can u say how to hardcode this and where.

Thanks
 
Old 06-14-2012, 04:48 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
do you have a makefile? can you post it?
 
Old 06-18-2012, 05:24 AM   #5
pradiptart
Member
 
Registered: Sep 2007
Posts: 102

Original Poster
Rep: Reputation: 12
Hi pan64,

I got your point,
I have tried something like -rpath and able to link it and run it but now I got another issue.
The issue is I am creating a RPM of the application so in the spec file only I am deciding where to put the libraries, which is not same as the path I have given during compilation in -rpath so,the problem is when it is running,the path of library is different and it is not running now so can u tell me how to resolve this error ,

as I don't know where the library will be as first compilation done then only the RPM creation done then how i will able to run this applications as library path is different now.

Kindly tell me this is a serious issue I am facing in my project work.

giving an example:

suppose I am working in /root/work/ folder and the makefile is here and all the source is here.all libraries are in /root/work_lib/ that I know during compilation and able to link this library during compilation,and able to run this
but during RPM creation I am deciding where to put the libraries of the RPM and that is different from this /root/work_lib/ and also that is variable depends upon the reqirements of projects ,so how do i able to make this application that will work after the RPM creation.

to this problem I got the old style LD_LIBRARY_PATH environment variable that I have to set in RPM spec file but is that the only one solution is there any other solution to this problem.

Thanks
 
Old 06-18-2012, 06:28 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
in such cases probably you can write a small shell script to set LD_LIBRARY_PATH and invoke your tool. Also you can use relative path during link: (instead of -L <path> -l <lib> simply write ../lib_dir/libsomething.so). In this case the app will look for the lib in ../lib_dir (which is probably not really good, but you can decide). You can try dynamic loader also to load a library.

_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
 
1 members found this post helpful.
Old 06-18-2012, 08:26 AM   #7
pradiptart
Member
 
Registered: Sep 2007
Posts: 102

Original Poster
Rep: Reputation: 12
hi ,
As you told dynamic loader,dlopen() function needs the library path that i can't give during compilation.
The problem is how to use the path that I do not know during compilation ,so I can't do any thing during compilation.
simply I have to build the RPM and then I have to do something by which the application should find the libraries.

The solutions to this problem are as follows

solutions
1.LD_LIBRARY_PATH
2?
3?

I need the other possible ways to do this.kindly tell about that .

Thanks
 
Old 06-18-2012, 08:33 AM   #8
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
Why don't you put the library inside a default path, like /usr/lib, /usr/local/lib etc.?
This way all your issues should be solved with no need for rpath and LD_LIBRARY_PATH...

Last edited by 414N; 06-18-2012 at 08:35 AM.
 
Old 06-18-2012, 08:36 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
Quote:
void *dlopen(const char *filename, int flag);
The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program. If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library
If you describe the filename with full path that will be used.
LD_LIBRARY_PATH can be set with a small wrapper script which will look for the lib and set the variable before executing your app. Both can work runtime, so in both cases they can use the path of the installed package.
 
  


Reply


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
[SOLVED] Linux: App Link Error with Shared Library - Varadic Macro Issue EmbeddedSteve Programming 3 02-28-2011 11:00 AM
[SOLVED] Link shared library against other shared library Lobinho Linux - Newbie 2 05-28-2010 09:16 AM
unable to link against a 'c' shared library vessper Linux - Software 4 11-09-2009 11:49 AM
gcc link shared library against another shared library qcp Linux - Newbie 1 07-25-2008 12:15 PM
Link shared library in Kdevelop neikinfology Linux - Software 0 07-12-2007 04:35 AM


All times are GMT -5. The time now is 06:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration