Building a Multi-threaded Program with Shared Libraries.

Dear experts,

I would like to write a multi-threaded program using shared libraries. There will be a front-end module calling external (kept as a file) shared libraries as separate threads. How can I achieve this, and I wonder how to create a Windows DLL like executables in Unix using C++.

Any document and comment welcomes.

Who is Participating?
It would be nice to know which version of *nix you're using and which compilers you're using.  We're just assuming you're using gcc on Linux.

You can't create Windows DLLs on Linux: the architecture is different and the concepts are different.  The program has to be compiled as multithreaded (there are 4 models on windows).  On Windows, a DLL is in 2 parts: a .lib for others to link with and a .dll for runtime.  The .dll can be either in the same directory as the executable or on the path.

On *nix, you probably need a -mt flag somewhere on the compile or the link line.  The way shared objects are approached is different, depending on which version of *nix you are using.  Some versions of *nix have their own multi-threading libraries, others use pthreads.  HPUX, for instance uses .sl instead of .so and, depending on whether you are using HPUX 10 or 11, it uses different environment variables.  Solaris used to have its own thread library and AIX is another different ball game.
DLL is Windows Term, In Linux they are called shared libraries.
See this:

You can create linux .so files.
Using gcc you can create a .so by using the -shared option.
gcc -shared -o foo.c
If you name your shared object lib*.so you can compile against it by using the `-l` option on your linker. Note that the "lib" is inferred in this circumstance. ie.
ld -o a.out -lfoo someobject.o

Alternatively you can load .so files at runtime, just as you can with .dlls, using dlopen() and dlsym().
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.