Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I link a DLL to my project? error LNK2019: unresolved external symbol

I have a file foo.h that has various declarations for functions. All of these functions are implemented in a file foo.dll. However, when I include the .h file and try to use any of the functions, I get the error:

bar.obj : error LNK2019: unresolved external symbol SomeFunction

so obviously the function implementations aren't being found.

What do I have to do to help the compiler find the definitions in the DLL and associate them with the .h file?

I've seen some stuff about __declspec(dllexport) and __declspec(dllimport) but I still can't figure out how to use them.

like image 547
xcdemon05 Avatar asked Feb 26 '13 13:02

xcdemon05


People also ask

How do I fix unresolved external symbol?

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.

What is LNK2019 unresolved external symbol?

LNK2019 can occur when a declaration exists in a header file, but no matching definition is implemented. For member functions or static data members, the implementation must include the class scope selector. For an example, see Missing Function Body or Variable.


2 Answers

You should have received at least three files from the DLL owner. The DLL which you'll need at runtime, the .h file with the declarations of the exported functions, you already have that. And a .lib file, the import library for the DLL. Which the linker requires so it knows how to add the functions to the program's import table.

You are missing the step where you told the linker that it needs to link the .lib file. It needs to be added to the linker's Input + Additional Dependencies setting of your project. Or most easily done by writing the linker instruction in your source code:

#include "foo.h"
#pragma comment(lib, "foo.lib")

Which works for MSVC, not otherwise portable but linking never is. Copy the .lib file to your project directory or specify the full path.

like image 75
Hans Passant Avatar answered Oct 12 '22 13:10

Hans Passant


I just had a similar problem. The solution turned out to be that the DLL was 64 bit, and the simple app using it was 32. I had forgotten to change it to x64 in the Configuration Manager.

like image 34
DarenW Avatar answered Oct 12 '22 11:10

DarenW