53

I'm using libcurl and am getting the following sort of linker errors in VC++ 10.

1>main.obj : error LNK2019: unresolved external symbol __imp__curl_easy_strerror referenced in function "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl curl_httpget(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?curl_httpget@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV12@@Z)

How can I get rid of that imp prefix in front of the function name? I am linking to the right lib, right path etc.

BeeBand
  • 10,953
  • 19
  • 61
  • 83
  • Related (or perhaps even duplicate) http://stackoverflow.com/questions/3704374/linking-error-lnk2019-in-msvc-unresolved-symbols-with-imp-prefix-but-should – Suma Mar 01 '11 at 19:44
  • Check out [this](https://dennisbabkin.com/blog/?t=intricacies-of-microsoft-compilers-the-case-of-the-curious-__imp_) and also [this](https://dennisbabkin.com/blog/?t=intricacies-of-microsoft-compilers-part-2-__imp_-and-__imp_load_-prefixes). – c00000fd Mar 01 '22 at 09:27

5 Answers5

81

The __imp__ prefix appears whenever you are linking to a DLL. It does not appear when linking to statically linked libraries. Most likely the code is generated to be linked against a DLL import lib, but you have linked it with a static lib instead.

The prefix is added when you mark the imported function with __declspec(dllimport) - make sure your imports are not using this when not linking against a DLL.

Chris Krycho
  • 3,125
  • 1
  • 23
  • 35
Suma
  • 33,181
  • 16
  • 123
  • 191
  • yes I linked it with a static lib, but how do I link against a dll import lib ( and where is this option to link to a .dll set? ). – BeeBand Mar 01 '11 at 19:36
  • 5
    Dll is accompanied with a .lib. You link against the dll by linking against this accompanying .lib. It is impossible to know by name if the .lib is a static lib or a dll "trampoline", to know this you need to check inside of the .lib, but the .lib needs to match your imports. – Suma Mar 01 '11 at 19:49
  • 2
    Thank you @Suma! In my case there was an ifdef which led to the `__declspec(dllimport)` even though it was a dependency for a dll. The dependency was build as static library, but that didn't affect the ifdef. – Pascal Feb 04 '17 at 20:11
22

You have to add CURL_STATICLIB to Preprocessor Definitions at the properties of your projects in MSVC

12

You are using a header file that defines the function prototype with the specifier evaluating to __declspec(dllimport)

You need to either redefine the statement that is evaluating to this (set it to nothing), or use a different header file altogether.

Typically you'll see code like this:

#ifdef FOO_EXPORTS
#define DLLSPEC __declspec(dllexport)
#else
#define DLLSPEC __declspec(dllimport)
#endif

...

DLLSPEC bool foo(int bar);

Compiling the project with FOO_EXPORTS defined will use one mode and without it will use the other.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
8

If using wizard generated projects - check "Runtime settings" value in project properties -> C/C++ -> Code Generation section.

By default it usually has "Multithreaded DLL" value. You need Multithreaded /MT and Multithreaded Debug /MTd values.

Alfishe
  • 3,430
  • 1
  • 25
  • 19
  • This worked when I was getting the error `LINK2019` `unresolved external symbol __imp_malloc` in Visual Studio 2015. It would compile without `#include ` or `#include ` and crash, but when I added those I got the unresolved external symbol issue. – douggard Feb 14 '17 at 17:11
0

As Suma mentioned, <your_library>.dll is normally accompanied by <your_library>.lib. Therefore if you get such error messages like

Error LNK2019 unresolved external symbol __imp__<your_symbol> referenced in function ...

it is highly recommended to check the Linker settings of your project.

In Visual Studio, just go for your project to

Properties -> Linker -> General

and check under entry Additional Library Directories, if the path to <your_library>.lib is defined there. If not, just add it.

Then check and define on

Properties -> Linker -> Input

under entry Additional Dependencies the probable missing <your_library>.lib.

The error message should disappear now.

Hope it helps?

CKE
  • 1,533
  • 19
  • 18
  • 29