Click to See Complete Forum and Search --> : Trouble making a shared library


PhatBarren
12-09-2002, 11:07 AM
Hi,

I'm trying to make a shared library (just to learn how to do it for now).

What I have is the following files:
sayhi.h
sayhi.c
main.c

And I do the following:

gcc -fPIC -c sayhi.c
gcc -shared -W1,-soname,libsayhi.so.1 -o libsayhi.so.1.0 sayhi.o
ln -s libsayhi.so.1.0 libsayhi.so.1
ln -s libsayhi.so.1 libsayhi.so

Then, I try to compile main.c:
gcc -shared -I. -L. -lsayhi main.c -ohello

Now, when I type ldd hello I get:
[lace@localhost lib]$ ldd hello
libsayhi.so => not found
libc.so.6 => /lib/i686/libc.so.6 (0x42000000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)

And when I try to run the program, I get Segmentation Fault.


I'm not sure what's wrong. I got all this from my c++ for linux book. The one thing in the compile commands that didin't make sence to me was the libsayhi.so.1 in
gcc -shared -W1,-soname,libsayhi.so.1 -o libsayhi.so.1.0 sayhi.o

Help is much appreciated.

Thanks,

bwkaz
12-09-2002, 02:30 PM
A couple of things. First, that -W1 should be a -Wl (dash, cap double-u, lowercase ell) instead.

Second, you need to put the shared library somewhere that the runtime linker can find it (which translates into somewhere in /lib, /usr/lib, /usr/local/lib, or anywhere else that's listed in your /etc/ld.so.conf file), and run /sbin/ldconfig as root, before ldd will be able to find it.

You could also set the LD_LIBRARY_PATH variable to ".", but that's not recommended because it doesn't always work (esp. for suid-root programs). If you do change LD_LIBRARY_PATH, then I don't think you have to run ldconfig.

Stuka
12-09-2002, 04:08 PM
bwkaz: I think he's gotten around the path issue - note the -L. flag for gcc.

bwkaz
12-09-2002, 05:36 PM
Nope, sorry. :D

Adding -L. to the gcc path doesn't tell gcc anything more than where to find the .so file on the compile system (gcc looks in a small set of predetermined paths, plus anything specified with -L).

It doesn't affect runtime linking at all, that I can tell. Programs that use e.g. /usr/X11R6/lib need that path in ld.so.conf at runtime, and they need the -L path at compile time. They work because that path is in ld.so.conf ;)

Stuka
12-09-2002, 06:03 PM
Ahh - I looked to quickly. I thought that was a compile time problem (teach me to skim!). ldconfig will definitely do the trick though ;)

PhatBarren
12-09-2002, 07:10 PM
Thanks for the responses.

I've moved my sayhi library to /usr/lib and now, after compiling, I get this output from ldd:

[lace@localhost lib]$ ldd hello
libsayhi.so.1 => /usr/lib/libsayhi.so.1 (0x40019000)
libc.so.6 => /lib/i686/libc.so.6 (0x42000000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)

But, I still get Segmentation fault when trying to run my program:
[lace@localhost lib]$ ./hello
Segmentation fault

I'm pretty sure my code is all correct, but here it is:

sayhi.h:

#ifndef __SAYHI_H
#define __SAYHI_H

void sayhi();

#endif


sayhi.c:

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

void sayhi() {
printf("Hi\n");
}

and main.c:

#include "sayhi.h"

int main() {
sayhi();
}


Do you know what's wrong now?

Thanks again,

PhatBarren
12-09-2002, 08:48 PM
Ok, I've found that if I don't use -shared in the final compile i.e. I use:
gcc -lsayhi main.c -ohello

rather than:
gcc -lsayhi main.c -ohello -shared

I can get an executable that works. And when I use ldd I still get:
[lace@localhost lib]$ ldd hello
libsayhi.so => /usr/lib/libsayhi.so (0x4002b000)
libc.so.6 => /lib/i686/libc.so.6 (0x42000000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

But, doesn't this mean that I'm not using the library in a shared way?

I tried compiling another simple program that does the exact same thing, but defines the sayhi() function internally, and it turned out to have a smaller size than the shared (13542 rather than 13711 for the shared).

I'm just not sure if it's actually working as a shared library or not, when I don't compile main.c with -shared.

bwkaz
12-10-2002, 10:51 AM
For that, I have no idea...

Anyone else know?

binaryDigit
12-10-2002, 05:30 PM
objdump -xT hello > tmp

DYNAMIC SYMBOL TABLE:
080494b0 g DO *ABS* 00000000 Base _DYNAMIC
08048344 g DF .init 00000000 Base _init
080495ac g D *ABS* 00000000 Base __bss_start
0804836c DF *UND* 000000d8 GLIBC_2.0 __libc_start_main
0804847c g DF .fini 00000000 Base _fini
0804837c DF *UND* 0000002a sayhi
080495ac g D *ABS* 00000000 Base _edata
08049594 g DO *ABS* 00000000 Base _GLOBAL_OFFSET_TABLE_
080495b0 g D *ABS* 00000000 Base _end
0804849c g DO .rodata 00000004 Base _IO_stdin_used
00000000 w D *UND* 00000000 _Jv_RegisterClasses
00000000 w D *UND* 00000000 __gmon_start__


yes it is shared.