Reputation: 2541
I'm writing a dll, which exports a function
extern "C"
__declspec(dllexport)
std::list<string> create() {
return std::list<string>();
}
the compiler complains:
error C2526: 'create' : C linkage function cannot return C++ class 'std::list<_Ty>'
if I remove the extern "C", the function name exported would be: ?create@@YA?AV?$list@PAUanalyzer@@V?$allocator@PAUanalyzer@@@std@@@std@@XZ
I want the name to be clean, so I added extern "C", now it conflicts
Any other way can get a clean function name?
Thanks.
Upvotes: 2
Views: 843
Reputation: 334
You can return a pointer instead of the object.
extern "C" __declspec(dllexport) std::list<std::string>* createAllocated()
{
return new std::list<std::string>();
}
Due to the nature of C++, the caller will need to have the compatible ABIs.
Upvotes: 1
Reputation: 409136
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.
Upvotes: 3