-   Programming (
-   -   Linux Dynamicly Linked Libraries (

sikil_nuru 03-13-2004 05:36 AM

Linux Dynamicly Linked Libraries
Hello! I was wondering what the counterpart for Windows DLLs is
under Linux, isn't it .SO or something? I know how to use DLLs under
Windows, but how do i use .SOs under Linux/other *NIX-systems?
Ive also heard that you can't place class-instances in .SOs, is that

(Im a Cpp-programmer with SuSe 9.0 )

// Alex

cjp 03-15-2004 02:09 PM

.so files are similar to .dll files in windows, yes. What do you want to do with them? You can decide to link to them at compile-time (actually, the linking step of course): just add -l options to the compile command (see man gcc). If you want to choose a .so file at run-time (e.g. a plugin that is selected by the user) then you can use dlopen (see man 3 dlopen).

I'm not sure how one should create .so files (I'm sure that google will find the answer). It was easy for me because I already knew things about automake, and automake has some features to set up the compilation of complex projects including the creation of .so files.

I did some things with C++ and dlopen, and I did have some problem. You must know that C++ does some weird things with the symbol names in order to make all the fancy C++ stuff work, but dlopen assumes C-style symbols. You can use "extern "C"" to force C-style symbols for some part of the .so file. For example:

class myclass : public baseclass
    //Real C++ stuff

extern "C"

baseclass *createObject()
  return new myclass;


johnMG 03-15-2004 02:35 PM

I recently wrote the original version of this wiki page:

Maybe it'll help.

To make use of .so's in your C++ code, just #include the header, and in your makefile
(or jamfile, or whatever ant uses -- depending on the build tool you're using) in your
makefile, for the linker arguments, just use

g++ -o my_app main.o -l foo
where foo represents the lib named (the space between -l and foo is optional).
You don't have to put the -lfoo at the end of the command line, that's just the way it's done
commonly. You could also specify the whole path
if you had to:

g++ -o my_app main.o -l/usr/local/lib/
See the man page for g++ for details on the most common args you'll be using:
-l, -L, -I.

Some libs even come with their own little utility to help you put the right g++ compile and
linker args in. For example, SDL comes with sdl-config. Example:

g++ -o my_sdl_GL_app main.o Spaceship.o -lglut -lGLU -lGL  `sdl-config --libs`

Don't know about the "class instances" question though. You might try
the newsgroup for that. Here's a link to the google archives:

cjp 03-16-2004 07:51 AM

The dlopen stuff & creating your own .so files are advanced programming stuff. If you're just a beginner and you just want to use one of these interesting libraries in /lib or /usr/lib, then all you have to do is include the include file and link with the .so file. For example if you want to use libmikmod for a mod player, then you should include mikmod.h and compile with
gcc -o myapp myapp.c -lmikmod
mikmod is the name of the file "", minus "lib" and minus ".so".

All times are GMT -5. The time now is 11:29 AM.