Visual 2015编译DLL

Visual 2015 Compiling DLL

我正在创建应作为模块(运行时加载)工作的 DLL
它适用于 linux/windows 与 dlopen 等..
看起来像:

.cpp

std::string pomnoz(std::string &s, std::string &ds)
{
    std::cout << s << "   " << ds << std::endl;
    return s.append(ds);
}

.h

#ifdef __cplusplus
extern "C"
{
#endif
    std::string pomnoz(std::string &s, std::string &ds);
#ifdef __cplusplus
}
#endif

问题是当我用 g++ 编译它时,它生成了 ~480kb .DLL,在 windows/linux 上没有问题(我传递了 2 个字符串,它 returns 它)。
但是我不能使用 g++,因为我在库中进一步使用了 c14。
在 windows 我使用的是 VS2015,它创建了 65kb 的 .DLL 不起作用(它加载但返回 null 而不是 funcptr)。
我会尝试删除 #ifdef __cplusplus
但一切都没有改变。

问题出在哪里?我应该在构建选项中切换一些东西吗?

您缺少 pomnoz 函数旁边的 dllexport:

__declspec(dllexport) std::string pomnoz(std::string &s, std::string &ds);

然后在您的应用程序中,您可以动态加载 dll 并检索导出函数的地址:

HMODULE lib = LoadLibrary(L"test.dll");

typedef std::string(*FNPTR)(std::string&, std::string&);
FNPTR myfunc = (FNPTR)GetProcAddress(lib, "pomnoz");

if (!myfunc)
    return 1;

std::string a("a");
std::string b("b");

myfunc(a, b);