Problem LNK2019

I have LNK2019 problem when trying to use some dll in my project.

More details:

  • I have a dll project called dll1; (using __declspec(dllexport) ) to export the class inside dll1 (for using dll2).
  • I have another dll2 dll project that uses dll1 functionality. I indicated the path of the * .dll1.lib file inside the linker entry in the project properties and gave a link to the dll1 * .h files. At this point, everything should work fine. (I think..)
  • When compiling dll2, I get an LNK2019 error that tells me that I cannot find any method mentioned in dll1. (This method in dll1 is a static method.)

Why am I getting this error?

+4
source share
4 answers

The MSDN page on LNK2019 already gives many examples of why this error occurs. To track what exactly is happening, I recommend doing the following:

  • Run undname on the undname with which the linker complains to expand the name (see Viewing decorated names for an example of how to start undname ).
  • Run dumpbin /EXPORTS (or use the Dependency Walker graphic) to get a list of all the characters exported by DLL1.

So now you have the demangled name of the character that the linker is trying to find, and you have a list of characters that are exported by DLL1. And the linker tells you that it cannot find the requested character in the list. Here are two ideas on what is going on:

  • You see that in the export list of DLL1 there is a demarched character, but not exactly a distorted name that the linker complains about. This can happen when the function you export is almost the same as the waiting linker. You may be missing a β€œconst” somewhere, or the calling convention is different.
  • You see that DLL1 does not export any character that looks like the linker expects. This suggests that some __declspec(dllexport) missing from __declspec(dllexport) .
+3
source

For ordinary static class methods, it is enough to specify declspec (dllexport), but in some cases (for example, built-in friendship functions) you need to provide declspec (dllexport) for this function.

eg.

 #define DLLEXPORT __declspec(dllexport) class DLLEXPORT A { A(); int somefunc(); DLLEXPORT friend int operator==(const A &ws1, const A &ws2) { /* some code */ } }; 
+2
source

Just a guess:

If you include the header from dll1 in the dll2 project, and you use __declspec(dllexport)) in this header, you tell the linker that dll2 also exports these classes, which are actually intended to be imported through dll2 , and therefore there is no class definition.

Thus, it would be convenient to use a definition such as this.

 #ifdef DLL1_EXPORTS #define DLLEXPORT __declspec(dllexport) #else #define DLLEXPORT __declspec(dllimport) #endif class DLLEXPORT A { //... 

This design ensures that dll1 definitions are exported when the header is used in dll1 and imported when used inside the dll2 project. All you need is the DLL1_EXPORT macro, which will be defined when dll1 is compiled . Project settings for dll1 are usually a good place.

Another approach is to have two different headers: one to build dll1 and the second to be used with wit dll1 lib (without any __declspec(dllexport) ).

+1
source

I had the same issue recently. The problem was the signature of the void myMethod(const MyType&); method void myMethod(const MyType&); with a parameter that was declared as class MyType; ; however, the definition was struct MyType{}; .

As described in fooobar.com/questions/419126 / ... , this can lead to linker problems with Visual C ++ - in my case LNK2019.

It took some time to solve this problem, perhaps it will help someone in the future.

0
source

All Articles