Friday, November 6, 2015

Shared Libraries Demo *.so file.



Ref 1 cmd line to generate *.so on stackoverflow
Ref 2 stackflow post ld --verbose -llibxxx

testsomain is smaller when not compiled with testso.c 
[q.yang@fedora20 Trash]$ gcc testsomain.c -o testsomain -L./ -ltestso
It's bigger when compiled with testso.c. It's one of the benefit of using dynamic library.
[q.yang@fedora20 Trash]$ gcc testsomain.c testso.c -o testsomain
testsomain.c
#include 
#include "testso.h"

int main(void)
{
  printf("Testing calling function in *.so \n");
  Ext_PrintHello();
}
testso.c
#include 

void Ext_PrintHello(void)
{
  printf("%s Invoked from external module.\n",__func__);
}
testso.h
//#error "this file has been included here"

void Ext_PrintHello(void);
Compile to generate dynamic library:
Note: MUST use -fPIC option PositionIndependantCode(PIC)
*.so file name must be libxxnamexx.so, cannot be xxnamexx.so
[q.yang@fedora20 Trash]$ gcc -shared -fPIC -o libtestso.so testso.c
Compile main program to load dynamic library *.so
[q.yang@fedora20 Trash]$ gcc testsomain.c -o testsomain -L./ -ltestso
Set load library search path
[q.yang@fedora20 Trash]$ LD_LIBRARY_PATH=./
[q.yang@fedora20 Trash]$ export LD_LIBRARY_PATH
Run program
[q.yang@fedora20 Trash]$ ./testsomain 
Testing calling function in *.so 
Ext_PrintHello Invoked from external module.
Checking so file dependencies $ldd and file info $file. (Running for i686 or ARM)
duser@10.1.1.8:~/Trash$ file libtestso.so 
libtestso.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, BuildID[sha1]=0xf54c8aad49c8dfd0220885eab40fe703c0e5495b, not stripped

duser@10.1.1.8:~/Trash$ ldd libtestso.so 
 libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb754b000)
 /lib/ld-linux.so.2 (0xb770f000)

We can also use linker $ld to check where system default search path for *.so shared library files. Given libtestso.so [Ref 2]
duser@10.1.1.8:~/Trash$ld -L./ -ltestso --verbose

..................
..................
==================================================
attempt to open /usr/i686-linux-gnu/lib32/libtestso.so failed
attempt to open /usr/i686-linux-gnu/lib32/libtestso.a failed
attempt to open /usr/local/lib32/libtestso.so failed
attempt to open /usr/local/lib32/libtestso.a failed
attempt to open /lib32/libtestso.so failed
attempt to open /lib32/libtestso.a failed
attempt to open /usr/lib32/libtestso.so failed
attempt to open /usr/lib32/libtestso.a failed
attempt to open /usr/local/lib/i386-linux-gnu/libtestso.so failed
attempt to open /usr/local/lib/i386-linux-gnu/libtestso.a failed
attempt to open /usr/local/lib/libtestso.so failed
attempt to open /usr/local/lib/libtestso.a failed
attempt to open /lib/i386-linux-gnu/libtestso.so failed
attempt to open /lib/i386-linux-gnu/libtestso.a failed
attempt to open /lib/libtestso.so failed
attempt to open /lib/libtestso.a failed
attempt to open /usr/lib/i386-linux-gnu/libtestso.so failed
attempt to open /usr/lib/i386-linux-gnu/libtestso.a failed
attempt to open /usr/lib/libtestso.so failed
attempt to open /usr/lib/libtestso.a failed
/usr/bin/ld.bfd.real: cannot find -ltestso
If we tell the load library path via -L option. Linker will find it.
duser@10.1.1.8:~/Trash$ ld -L./ -ltestso --verbose
....................
....................
==================================================
attempt to open .//libtestso.so succeeded
-ltestso (.//libtestso.so)
libc.so.6 needed by .//libtestso.so
found libc.so.6 at /lib/i386-linux-gnu/libc.so.6
ld-linux.so.2 needed by /lib/i386-linux-gnu/libc.so.6
found ld-linux.so.2 at /lib/i386-linux-gnu/ld-linux.so.2
/usr/bin/ld.bfd.real: warning: cannot find entry symbol _start; not setting start address

No comments: