sil2100//vx developer log

Welcome to my personal web page


g++ and C++ - class method definitions and shared objects

During work on some things regarding my master thesis, I stumbled upon something I did not know about before. I'm not much of a reverse engineer, and I don't really have the time to look into it closer.


I wanted to create a shared object file using a class of my creation. I wanted to export an already defined object of that class to be accessible through the applications dynamically linking the object. Consider the following example:

#include <iostream>

class ala {
        int test(void) {
            return 1;

extern "C" {
    ala test;

Those who had to deal with .so files in Linux systems already know that there is a difference in the way how symbols are stored in C and C++. C++ names are mangled to support function overloading, so for us to be able to correctly access a variable we need to use the extern "C" qualifier.
Our example should work perfectly fine. We can now dlopen() the object file and dlsym() the symbol test. Everything is as it should. Virtualization is also fine, as long as there are no loose ends on both classes. What should be remembered: you cannot create class objects from a shared object using the default new operator. Creation of new objects needs to be done in shared object code, e.g. using wrapper functions. But this you probably already know while reading other articles on the internet.

I don't use C++ too much. What I did not know is that there is a slight difference between defining a method inside the class body and defining it outside, leaving just the declaration inside. In the first case, when the g++ compiler doesn't see the method being used, it doesn't seem to be included in the binary at all.

Interesting. But maybe it's just a coincidence? I would have to check gcc source code to be sure.