Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 576
Flame Avatar asked Sep 12 '08 00:09

Flame


People also ask

Can you use CPP libraries in C?

If the C++ compiler provides its own versions of the C headers, the versions of those headers used by the C compiler must be compatible. Oracle Developer Studio C and C++ compilers use compatible headers, and use the same C runtime library. They are fully compatible.

How do you load a class in C++?

The ClassLoader loads C++ classes from shared libraries at runtime. It must be instantiated with a root class of the loadable classes. For a class to be loadable from a library, the library must provide a Manifest of all the classes it contains.

What is C++ .so file?

The basics of using a Shared Library file A file with the . SO file extension is a Shared Library file. They contain information that can be used by one or more programs to offload resources so that the application(s) calling the SO file doesn't have to actually provide the file.


2 Answers

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.

like image 75
James Allman Avatar answered Sep 28 '22 06:09

James Allman


My solution/testing

Here's my solution and it does what i expected.

Code

cat.hh :

#include <string>

class Cat
{
    std::string _name;
public:
    Cat(const std::string & name);
    void speak();
};

cat.cpp :

#include <iostream>
#include <string>

#include "cat.hh"

using namespace std;

Cat::Cat(const string & name):_name(name){}
void Cat::speak()
{
    cout << "Meow! I'm " << _name << endl;
}

main.cpp :

#include <iostream>
#include <string>
#include "cat.hh"

using std::cout;using std::endl;using std::string;
int main()
{
    string name = "Felix";
    cout<< "Meet my cat, " << name << "!" <<endl;
    Cat kitty(name);
    kitty.speak();
    return 0;
}

Compilation

You compile the shared lib first:

$ g++ -Wall -g -fPIC -c cat.cpp
$ g++ -shared -Wl,-soname,libcat.so.1 -o libcat.so.1 cat.o

Then compile the main executable or C++ program using the classes in the libraries:

$ g++ -Wall -g -c main.cpp
$ g++ -Wall -Wl,-rpath,. -o main main.o libcat.so.1 # -rpath linker option prevents the need to use LD_LIBRARY_PATH when testing
$ ./main
Meet my cat, Felix!
Meow! I'm Felix
$
like image 44
Flame Avatar answered Sep 28 '22 04:09

Flame