开发者

How to export C++ functions with GCC?

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 Flo开发者_如何学运维w::Render::IRender();
    }
}

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


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜