0

In an attempt to export functions from my dll I'm getting error C2526: 'LoadFromFile': C linkage function cannot return C++ class. I'm using extern "C" to unmangling function names.

Dll's header file:

extern "C" __declspec(dllexport) std::unordered_map<std::string, std::string> LoadFromFile(const std::string& path);

I could always pass a pointer to an std::unordered_map in the arguments, but is there a workaround for this?

I thought about using the offsets in the map file, but I figure it would be unreliable as significant changes would change those offsets. Or return to using /EXPORT in the command line.

Kookehs
  • 84
  • 5
  • 3
    While I know people do this all the time, I'm not a fan of crossing DLL boundaries with std:: C++ objects. Imagine if your DLL links with the version of msvcr120.dll that shipped with Visual Studio 2013. My executable that links with your DLL was linked with msvcr130.dll that ships with Visual Studio 2015. And another customer is statically linking with the MSVC library.Our version of std::unordered_map might be slightly different than what your DLL returns. Undefined behavior at best. Same holds true for file handles and malloc calls, then closed/freed in the other module. – selbie Dec 20 '15 at 06:58
  • What makes you think you can pass C++ objects via "extern C" interfaces? – John Zwinck Dec 20 '15 at 06:59
  • But if all the consumers of your DLL link with the same version of MSVCR.dll and ship together, you are perfectly fine. – selbie Dec 20 '15 at 06:59
  • 1
    `extern "C"` won't prevent name mangling anyway. [It just means you get C name mangling instead of C++ name mangling.](http://blogs.msdn.com/b/oldnewthing/archive/2012/05/25/10310148.aspx) – Harry Johnston Dec 20 '15 at 07:04

0 Answers0