Visual 2015 Compiling DLL



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

. 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个字符串,它返回它)。
但是我不能使用g++,因为我在库中进一步使用c14。
在windows上,我使用VS2015,它创建了65kb . dll不工作(它加载但返回null而不是funcptr)。
我将尝试删除#ifdef __cplusplus
但一切都没有改变。

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

您缺少pomnoz函数旁边的lexport:

__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);

最新更新