views:

545

answers:

3

I am writing a program in C++ using Eclipse. I want to compile it as a library for Linux, somthing like a DLL in Windows. How I can do this? Do you know any tutorials on how libraries are created?

I just want to understand that is the analog of a DLL for Linux and how to create it. I will be thankful for a small example.

+6  A: 

In UNIX/Linux world DLLs are called shared libraries and typically have .so or .o extension.

See Linux HOWTO on shared libs.

qrdl
.a are not shared, but static libraries
jpalecek
I meant ".o", not ".a" (corrected). AIX uses ".o".
qrdl
,o files are not shared libraries - they are object files
anon
AIX used ".o" for both object files and shared libs. At least it was so in AIX 4.x, don't remember about 5.
qrdl
+8  A: 

In Linux, DLL's equivalents are (kind of anyway) shared objects (.so).

You need to do something like this:

$ g++ -c -fPIC libfile1.cpp
$ g++ -c -fPIC libfile2.cpp
$ g++ -shared -o libyourlib.so libfile1.o libfile2.o

Take a look at some open source C++ library projects for more information. GTKMM is one of them.

Of course, instead of compiling everything manually it's highly recommended to use a make file or an IDE (such as Eclipse with CDT or KDevelop or {pick your favorite here}) that will create one for you behind the scenes.

Pablo Santa Cruz
You should add the -fpic or -fPIC option to the "g++ -c ..." calls. This is to generate "position independent code" so Linux can share the same code in memory between several processes.
rstevens
Thanks! Forgot about that.
Pablo Santa Cruz
A: 

You may want to change the default visibility of symbols. It can improve performance. Check out the GCC wiki entry on the subject.

There is also a paper written by Ulrich Drepper in 2006 that describes the proper way to implement dynamic shared objects on UNIX systems.

Dan