3

我正在使用 Code::Blocks 在 Ubuntu 上编译共享库。当我制作一个简单的 main.c 文件时:

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

这工作正常,我可以在另一个应用程序中找到带有 dlsym 的函数 CreateInterface。但是,我希望该函数创建一个用 C++ 编写的类的实例。我尝试了以下方法:

#include "IRender.h"

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

这编译得很好,但现在我的其他应用程序找不到 CreateInterface。我应该如何处理?

4

1 回答 1

2

我通过制作一个带有声明的 .cpp 文件解决了这个问题:

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

和一个带有如下标题的 .c 文件:

extern void* CreateInterface();
于 2010-04-08T13:23:53.293 回答