LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-15-2018, 04:08 PM   #1
jose_spain
LQ Newbie
 
Registered: Apr 2018
Posts: 8

Rep: Reputation: Disabled
C/C++ shared libraries on Linux.


This is the first time that I created a dynamic library in linux and although the program works, I do not get the correct information about the library when executing ldd.

I explain the details:

1) Source code:

bye_fn.c:

#include <stdio.h>
#include "hello.h"

void bye (const char* name)
{
printf ("\n\n%s, I see you soon !\n\n", name);
}


hello_fn.c:

#include <stdio.h>
#include "hello.h"

void hello (const char* name)
{
printf ("\n\nHello, %s !\n\n", name);
}


hello.c:

#include "hello.h"
#include <stdio.h>

int main (void)
{
hello ("Josť");
printf ("Please to meet you. You will work with us. You start next week. Congratulations !\n");
bye ("Josť");

return 0;
}


2) Steps followed to obtain the shared library and the executable program:

gcc -fPIC -Wall -c hello_fn.c

gcc -fPIC -Wall -c bye_fn.c

gcc -shared -Wall -o libhello.so hello_fn.o bye_fn.o

gcc hello.c libhello.so -o hello


3) Steps followed to execute the "hello" program:

export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:./

./hello

It works right.


4) My question:

If I execute the command: ldd hello
the output is:

linux-vdso.so.1 => (0x00007ffc1db7c000)
libhello.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007eff63dd2000)
/lib64/ld-linux-x86-64.so.2 (0x00007eff6419b000)

I would to get correctly the information about the library "libhello.so" that is being used by "hello".

What should I change or add in the steps described in point 2, to achieve this?
 
Old 04-15-2018, 05:23 PM   #2
RandomTroll
Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 931

Rep: Reputation: 151Reputation: 151
A DLL has to be in a directory in /etc/ld.so.conf; after you create a new library to one of those directories you have to update ld.so.cache by running ldconfig.
 
1 members found this post helpful.
Old 04-15-2018, 05:37 PM   #3
jose_spain
LQ Newbie
 
Registered: Apr 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by RandomTroll View Post
A DLL has to be in a directory in /etc/ld.so.conf; after you create a new library to one of those directories you have to update ld.so.cache by running ldconfig.
Thanks.

I have tried it in three different ways:

1) I have executed on the directory where the library "libhello.so" and the program "hello" are, the command:

ldconfig -vn ./

But the problem follows.



2) Also I have copied the "libhello.so" file to "/home/jose/lib" and then executed the command:
sudo ldconfig -vn /home/jose/lib

Then I execute the command:
ldconfig -v | head

But the library "libhello.so" does not appear.


3) I have copied "libhello.so" to "/etc/ld.so.conf.d/
Then I have executed: sudo ldconfig

But when I execute again: ldconfig -v | head

The new library does not appear.


What is wrong?

Last edited by jose_spain; 04-15-2018 at 06:33 PM.
 
Old 04-15-2018, 06:57 PM   #4
jose_spain
LQ Newbie
 
Registered: Apr 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by jose_spain View Post
Thanks.

I have tried it in three different ways:

1) I have executed on the directory where the library "libhello.so" and the program "hello" are, the command:

ldconfig -vn ./

But the problem follows.



2) Also I have copied the "libhello.so" file to "/home/jose/lib" and then executed the command:
sudo ldconfig -vn /home/jose/lib

Then I execute the command:
ldconfig -v | head

But the library "libhello.so" does not appear.


3) I have copied "libhello.so" to "/etc/ld.so.conf.d/
Then I have executed: sudo ldconfig

But when I execute again: ldconfig -v | head

The new library does not appear.


What is wrong?
Thanks, I have found the solution.

The "libhello.so" file must be copied to "/usr/lib", then execute "ldconfig" and re-link the executable.
 
Old 04-15-2018, 10:31 PM   #5
RandomTroll
Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 931

Rep: Reputation: 151Reputation: 151
Also you could have added /home/jose/lib to /etc/ld.so.conf then run ldconfig.

If your problem is solved you should mark this thread as solved.
 
Old 04-16-2018, 09:24 AM   #6
jose_spain
LQ Newbie
 
Registered: Apr 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Adding my lib path to ld.so.conf does not work.

Following these steps all work right:
1) I copy "libhello.so" to "/usr/lib/"
2) sudo ldconfig
3) Re-link with:
gcc hello.c -lhello -o hello
4) Run the program:
./hello
5) "ldd hello" works fine:
linux-vdso.so.1 => (0x00007ffc9ab50000)
libhello.so => /usr/lib/libhello.so (0x00007f9aecc59000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9aec890000)
/lib64/ld-linux-x86-64.so.2 (0x00007f9aece5a000)

But if I copy "libhello.so" to "/home/jose/lib" and add this to "/etc/ld.so.conf" it does not work

Initally, the content of this file is:
include /etc/ld.so.conf.d/*.conf

I have tried to add:
include /home/jose/lib/

Or to add:
include /home/jose/lib/hello.so

Or to add:
/home/jose/lib/

Or to add:
/home/jose/lib/hello.so

After this I excute "lddconfig" and re-link "hello" but in any case, it never works.

What is wrong?
 
Old 04-16-2018, 12:16 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
You can add customs .conf files in /etc/ld.so.conf.d

so you could write, say /etc/ld.so.conf.d/jose.conf
With content:
Code:
/home/jose/lib
then execute ' ldconfig -v ' and see output..

Last edited by keefaz; 04-16-2018 at 12:17 PM.
 
1 members found this post helpful.
Old 04-16-2018, 04:56 PM   #8
jose_spain
LQ Newbie
 
Registered: Apr 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Many thanks.

If I do it so and execute:
sudo ldconfig -v
the output is:
. . .
/home/jose/lib:
libhello.so -> libhello.so
. . .


But when I re-link the program hello with the command:
gcc hello.c -lhello -o hello

The next error is shown:
/usr/bin/ld: cannot find -lhello
collect2: error: ld returned 1 exit status
 
Old 04-16-2018, 05:24 PM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
Yes ld.so is a dynamic linker/loader, its job is to
Quote:
find and load the shared objects (shared libraries) needed by a program
See man ld.so

So for linking library at compile time, if library is in non standard path you have to specify library directory with gcc -L option
Code:
gcc hello.c -lhello -L/home/jose/lib -o hello
 
  


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
./testSo: error while loading shared libraries: libsy.so: cannot open shared object rohaanembedded Programming 5 06-03-2013 07:23 AM
error while loading shared libraries: libhid.so.0: cannot open shared object file: No misungs Linux - Software 4 06-10-2009 12:05 PM
linux init error in loading shared shared libraries akaran Linux - Software 1 05-28-2003 04:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:12 AM.

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