Unresolved symbol errors within DLL
Asked Answered
T

1

10

For background, I have come across this porting a medium-sized linux codebase (compiling into a giant .so) to x64 windows (compiling into a .dll). I have had linker trouble.

As a minimal testcase, if I create a Visual Studio project from just the following file:

#include <Windows.h>
#include <Dbghelp.h>

void do_stuff(char const * s)
{
  char buffer[4096];
  long int len = UnDecorateSymbolName(
    s,
    buffer,
    sizeof(buffer),
    UNDNAME_COMPLETE);
}

And I set the project type to DLL and build it, I get an error "LNK2001: Unresolved external symbol __imp_UnDecorateSymbolName". That is, the file compiles properly, but fails to link into a dll.

I think the goal is for my dll to link to dbghelp.dll, especially since (at least on my system) there is no such file as a dbghelp.lib. So why is it trying to resolve that symbol now, rather then when my DLL is loaded into an application? And why can't it see that function anyhow?

To be clear, I have confirmed that I am building the x64 DLL, and that the dbghelp.dll in C:\Windows\System32 is x64.

Tempa answered 1/3, 2012 at 19:41 Comment(1)
It still needs to link with the import library dbghelp.lib; this should be included with Visual Studio, Platform SDK, etc.Lowercase
A
18

Linking to shared libraries, DLLs in Windows-speak, requires the following:

  1. A header file at compile time: Dbghelp.h.
  2. An import library at link time: Dbghelp.lib.
  3. A DLL at runtime: Dbghelp.dll.

You clearly have 1 and 3 and are missing 2. The Windows SDK that comes with Visual Studio includes the import library. But you need to add it as an additional dependency in your project's linker options.

Like this:

enter image description here

Albric answered 1/3, 2012 at 20:30 Comment(4)
That was it. My windows-search-fu was weak, my system does have the dbghelp.lib. Adding it to the dependencies worked like a charm.Tempa
Dbghelp is one of the more arcane libraries in Windows. There have been a huge number of different versions released and many released out of band with the OS. It's very easy to get into a situation where you are calling functions that present on your dev machine but not present on plain vanilla target machines. Just be careful!Albric
or you could #pragma comment(lib, "dbghelp.lib")Oleo
I especially like @IonTodirel commentDeonnadeonne

© 2022 - 2024 — McMap. All rights reserved.