Hey, Forum!
To put a long story short, I get this error from testing DLLs:
undefined reference to `_imp___Z10AddNumbersdd'
Why?
Oh, and this is the DLL code:
1 2 3 4 5 6 7 8 9 10 11 12
#include <windows.h>
BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID lpReserved)
{
return TRUE;
}
__declspec(dllexport) double AddNumbers(double a, double b)
{
return a + b;
}
#include <Windows.h>
#include <iostream>
// declare your function
typedefdouble(__cdecl *AddNumbersProc)(double, double);
AddNumbersProc AddNumbers;
int main() {
HINSTANCE lib = LoadLibrary("maths.dll"); // or whatever the DLL is called
if (!lib) {
// failed to load DLL; make sure it exists
}
// Replace "_Z10AddNumbersdd" with whatever the DLL actually exported the
// name as; that's just my guess of what it could be. You can find the real name
// of the function in the .def file that was probably exported with the DLL.
AddNumbers = (AddNumbersProc)GetProcAddress(lib, "_Z10AddNumbersdd");
if (!AddNumbers) {
// failed to load function; make sure you got the name right
}
// now you can use it like a function!
std::cout << AddNumbers(5, 4) << '\n';
return 0;
}
I wrote an article on this very topic a couple of years ago. It's not brilliant, but it does have examples which you can work from if you wish: http://www.cplusplus.com/articles/48TbqMoL/
dllexport/dllimport is for automatically dll loading. You don't need LoadLibrary(...). What you need is linking against a lib created with the dll. Which would resolve the linker error.
If you want to use LoadLibrary(...) you don't need dllexport/dllimport.