views:

84

answers:

2

Hello experts,

  1. Is the STD library a shared library or what is it ? out of curiosity .
  2. Are there any books describe in detail the shared , static libraries development ?
  3. Are there any tutorial ?

p.s (i'm using netbeans , eclipse, anjuta) and the tutorials aren't useful as I'm trying to understand what's actually going on.

+5  A: 

On my platform (Ubuntu Maverick) it is:

g++ test.cpp
ldd a.out

linux-vdso.so.1 =>  (0x00007fffee1ff000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f18755fd000)
libm.so.6 => /lib/libm.so.6 (0x00007f187537a000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007f1875163000)
libc.so.6 => /lib/libc.so.6 (0x00007f1874de0000)
/lib64/ld-linux-x86-64.so.2 (0x00007f1875920000)

Note libstdc++.so.6 above.

With cmake creating a shared library is very easy.

1. Install cmake 2.6 or later.

2. Create a file test.cpp with the code for your library.

3. Create a file CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)
project(TEST)
add_library(test SHARED test.cpp)

4. Run cmake to create a makefile:

cmake -G "Unix Makefiles"

5. Run make to build your shared library.

With CMake you can also generate an Eclipse CDT project using the following command

cmake -G "Eclipse CDT4 - Unix Makefiles"

You can also find an interesting article on the topic with further references here.

vitaut
thanks for the quick answer
ismail marmoush
+2  A: 

1.) Is the STD library a shared library or what is it?

I have no idea. Could be either. Probably both. Does it matter? Unless you are dealing with something really exotic like a stand-alone statically linked binary for system rebuilding, as long as the compiler/system knows how to link it in, you are unlikely to be concerned with it.


In a nutshell, code can be in static libraries, in which case it's linked into the final (compiled/generated) executable and those binaries can become quite large. Or it can be in a shared library, in which case the library is dynamically loaded and multiple applications can (theoretically) share one common memory image. Unless you are doing something that is quite large, and that will be shared across multiple applications, I'd question the wisdom of going with shared libraries. The additional headaches, especially debugging headaches, are rarely worth it. And without multiple concurrently running applications, there's no savings...


To make a static library, I'd compile a bunch of files into object files... Than use ar and randlib. E.g.:

g++ -c foo1.C -o foo1.o
g++ -c foo2.C -o foo2.o
ar -rv libfoo.a foo1.o foo2.o
ranlib libfoo.a

Subsequently, I'd just link that library in:

g++ testfoo.C -o testfoo -L. -lfoo

Note that if you are using multiple libraries, the ordering of -lbar1 -lbar2 on that (g++ testfoo.C) command line is important! It determines which libraries can call functions/methods in other libraries. Circular dependencies are BAD!

With respect to foo1.o foo2.o files to ar, the ordering makes no difference.


Dynamic libraries...

Some time ago, under an ancient fedora core 3 system, I was playing around with shared libraries under linux. Back then, I would compile my shared library, say fooLibrary.c, with:

g++ -shared -Wl,-soname,libfooLibrary.so.1 -o libfooLibrary.so.1.0 -fPIC fooLibrary.c -ldl

At that time I was playing with LD_PRELOAD, so I had a little script to run my program that did:

export LD_PRELOAD=libfooLibrary.so ; export LD_LIBRARY_PATH=. ; ./myTestProgram

(Note that I did NOT want LD_PRELOAD set when running commands like g++, ls, cd, etc as I was intercepting system calls.)

(FYI: strace is also fun to play with... You should also check out ldd and nm.)

You may want to look at things like dlopen() and dlsym() -- for manually accessing dynamic libraries...

Oh, and the environment variable LD_LIBRARY_PATH adds directories to the default searchpath for dynamic libraries...

(With respect to debugging, let me just mention that when I intercepted malloc(), I found that somewhere inside dlopen()/dlsym() were calls to malloc(). Meaning that I needed to use malloc() before I could manually load the library that provided the real malloc(). Fun times debugging that one...)


PS One more thought: You may want to review the command-line options to gcc/g++. There's a lot of useful info in there...

http://gcc.gnu.org/onlinedocs/gcc-4.5.1/gcc/index.html#toc_Invoking-GCC

Mr.Ree