1
votes

I'm creating a DLL project with Visual Studio 2012 with the intent of calling the functions from a Delphi project. I've already turned off C++ name mangling by wrapping my functions in extern "C" { /* ... */ } but to avoid the C name decoration, I'm using a DEF file.

Here's a snippet of code describing how I have things laid out:

// MyFile.h

#ifdef MYPROJECT_EXPORTS
#define MYPROJECT_API __declspec(dllexport)
#else
#define MYPROJECT_API __declspec(dllimport)
#endif

extern "C" {
    extern MYPROJECT_API void __stdcall do_something();
}

The .cpp file is equivalently structured. Everything matches fine.

I added a .def file to my project folder containing the following content:

LIBRARY MyProject
EXPORTS
    do_something=_do_something@0

When I build my project, everything works okay. However when I add the linker option

/DEFS:MyProject.def

I start getting errors regarding unresolved external symbols. Has anybody had this kind of issue before? I'm not new to either C++ or C but this is my first time building a DLL on Windows, as I'm primarily a Unix developer. My hypothesis is that I'm either misunderstanding or misusing the __declspec decorator, as it only breaks when I link in the def file: when I remove that flag from the linker options it builds cleanly, except with C-style decorated symbols.

EDIT: Thanks for the comments everyone, but I've gotten myself figured out now. My issue came from the fact that I was using the def file during the linking phase during creation of my DLL. I need to use the def file when using the DLL, not creating it, and everything is peachy.

1
I guess the linker doesn't find _do_something@0. Have you tried leaving just do_something or do_something=do_something in your EXPORTS section (instead of =_do_something@0)?nullptr
I believe you want to use the .DEF file or the __declspec, but not both.Adrian McCarthy
Nothing obviously wrong. Then again, we can't see the linker errors. Don't make us guess at them.Hans Passant
It turns out I was confused about what was going on where. I don't need to use the def file when linking the actual DLL, but when using it from Delphi.bstamour
Post your solution as an answer.ChrisF

1 Answers

1
votes

Thanks for the comments everyone, but I've gotten myself figured out now. My issue came from the fact that I was using the def file during the linking phase during creation of my DLL. I need to use the def file when using the DLL, not creating it, and everything is peachy.