Search code examples
c++dllimportdllexport

dllexport functions not exported


I have done everything according to MSDN documentation and still my functions do not get exported. LINK1 LINK2 LINK3

Here is what I have (2 projects, DLL and App importing from the dll) which looks like this (short version):

DLL functions.h

#prgma once
#ifdef COMPILE_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif // COMPILE_DLL

namespace wsl
{
        EXPORT bool PointInTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y);
}

DLL functions.cpp

#include "functions.h"

namespace wsl
{
    bool PointInTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
    {
         // implementation...
         return true;
    }
}

DLL dllmain.cpp

#include <Windows.h>
BOOL APIENTRY DllMain(
    [[maybe_unused]] HMODULE hModule,
    DWORD  ul_reason_for_call,
    [[maybe_unused]] LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

NOTE: there are several other headers and cpp files in dll project, all being compiled, but no function is exported.

Application main.cpp

#include "functions.h"

int main()
{
    wsl::PointInTriangle(0, 0, 20, 0, 10, 30, 10, 15);
    return 0;
}

Program output:

2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) bool __cdecl wsl::PointInCircleSector(int,int,int,float,float)" (__imp_?PointInCircleSector@wsl@@YA_NHHHMM@Z) referenced in function main 2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) bool __cdecl wsl::PointInTriangle(int,int,int,int,int,int,int,int)" (__imp_?PointInTriangle@wsl@@YA_NHHHHHHHH@Z) referenced in function main 2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) bool __cdecl wsl::PointInEllipse(int,int,int,int,int,int)" (__imp_?PointInEllipse@wsl@@YA_NHHHHHH@Z) referenced in function main 2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) bool __cdecl wsl::PointInCircle(int,int,int,int,int)" (__imp_?PointInCircle@wsl@@YA_NHHHHH@Z) referenced in function main 2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) bool __cdecl wsl::PointInRectangle(int,int,int,int,int,int)" (__imp_?PointInRectangle@wsl@@YA_NHHHHHH@Z) referenced in function main 2>C:\Users\User\source\repos\WindowsSuperLibrary\x64\Debug DLL\MathSample.exe : fatal error LNK1120: 5 unresolved externals 2>Done building project "MathSample.vcxproj" -- FAILED.

Can you please explain what is the problem here?

I've run dumpbin /EXPORTS against the dll and can confirm no functions are exported.

COMPILE_DLL macro is of course defined in dll project.

I did include import library into my project.

namespace name is the same in both header and cpp file.


Solution

  • In order for a function declared in a source file for a DLL to actually be exported, you must do one of two things:

    Either (1) add the __declspec(dllexport) attribute to the function in the source where it is defined (not just in a header).

    Or (2) Add the function to the list of EXPORTS in a Module Definition File (.def).

    Available documentation (especially older stuff) tends to assume the use of the .def file EXPORTS method, and can be quite misleading.