views:

76

answers:

1

I'm using Code::Blocks to compile a shared library on Ubuntu. When I make a simple main.c file with:

void* CreateInterface()
{
    int* x = (int*)malloc( sizeof( int ) );
    *x = 1337;
    return x;
}

This works fine and I can find the function CreateInterface with dlsym in another application. However, I want the function to create an instance of a class written in C++. I tried the following:

#include "IRender.h"

extern "C"
{
    void* CreateInterface()
    {
        return new Flow::Render::IRender();
    }
}

This compiled fine, but now my other application fails to find CreateInterface. How should I deal with this?

+1  A: 

I've solved the problem by making a .cpp file with the declaration:

extern "C" void* CreateInterface()
{
    return new Flow::Render::IRender();
}

and a .c file with the header like this:

extern void* CreateInterface();
Overv