1

我正在写一个 dll,它导出一个函数

extern "C"
__declspec(dllexport)
std::list<string> create() {
return std::list<string>();
}

编译器抱怨:

错误 C2526:“创建”:C 链接函数无法返回 C++ 类“std::list<_Ty>”

如果我删除外部“C”,导出的函数名称将是:?create@@YA?AV?$list@PAUanalyzer@@V?$allocator@PAUanalyzer@@@std@@@std@@XZ

我希望名称干净,所以我添加了 extern "C",现在它冲突了

还有其他方法可以获得干净的函数名称吗?

谢谢。

4

2 回答 2

3

When you say extern "C" you tell the compiler to create a function that can be called from other languages than C++ (most notably C). However, no other language than C++ have std::list, so it can not create such a function because the return type is part of the function signature in C++. And if you don't create a function which has C-compatible return type (or arguments) you can not create an extern "C" function.

If you're going to use the DLL from a C++ program there is no need for the extern "C" part. The C++ compiler and linker will be able to handle the mangled name without problems anyway.

于 2013-11-11T05:36:36.790 回答
0

您可以返回指针而不是对象。

extern "C" __declspec(dllexport) std::list<std::string>* createAllocated()
{
    return new std::list<std::string>();
}

由于 C++ 的性质,调用者需要具有兼容的 ABI。

于 2020-12-28T15:49:39.997 回答