DLLs and undefined reference to...

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;
}


Builder macro:
1
2
3
4
5
#ifdef BUILDING_DLL
#define DLL_FUNCTION __declspec(dllexport)
#else
#define DLL_FUNCTION __declspec(dllimport)
#endif 


Main code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <windows.h>
#include <stdio.h>
#include <iostream>
#define BUILDING_DLL.h


HMODULE WINAPI LoadLibrary(LPCTSTR add);

__declspec(dllimport) double AddNumbers(double a, double b);

using namespace std;

int main()
{
    cout << AddNumbers(5,4);
}


IDE:
Code::Blocks

Compiler:
GNU GCC
You haven't actually loaded the DLL. All you've done is declared that somewhere there is a function AddNumbers.

What you need to do is you need to use LoadLibrary in main to store your function in a pointer. So:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <Windows.h>
#include <iostream>

// declare your function
typedef double(__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/
What are you trying to do?

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.
Topic archived. No new replies allowed.