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.
The compiler can identify when a symbol isn't declared, but it can't tell when the symbol isn't defined. That's because the definition may be in a different source file or library. If a symbol is referred to but never defined, the linker generates an unresolved external symbol error.
So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.
To fix this issue, add the /NOENTRY option to the link command. This error can occur if you use incorrect /SUBSYSTEM or /ENTRY settings in your project. For example, if you write a console application and specify /SUBSYSTEM:WINDOWS, an unresolved external error is generated for WinMain .
Answer. Unresolved external references occur when the symbol for a function or global variable is referenced in a program, but none of the object files or libraries specified in the link step contain a definition for that symbol.
Linking to shared libraries, DLLs in Windows-speak, requires the following:
Dbghelp.h
.Dbghelp.lib
.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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With