Flame Flame - 6 months ago 27
Linux Question

Using C++ classes in .so libraries

I'm trying to write a small class library for a C++ course.

I was wondering if it was possible to define a set of classes in my shared object and then using them directly in my main program that demos the library. Are there any tricks involved? I remember reading this long ago (before I started really programming) that C++ classes only worked with MFC .dlls and not plain ones, but that's just the windows side.

Answer

C++ classes work fine in .so shared libraries (they also work in non-MFC DLLs on Windows, but that's not really your question). It's actually easier than Windows, because you don't have to explicitly export any symbols from the libraries.

This document will answer most of your questions: http://people.redhat.com/drepper/dsohowto.pdf

The main things to remember are to use the -fPIC option when compiling, and the -shared option when linking. You can find plenty of examples on the net.

Comments