Lelo
Lelo

Reputation: 912

How to import functions using dllImport when they were exported via .def file?

I'm having a hard time compiling Minizip's (http://www.winimage.com/zLibDll/minizip.html) example source code into a console application in Windows.

While trying to build the minizip.c example, I'm getting the following errors:

Error   9   error LNK2019: unresolved external symbol _zipClose referenced in function _main    C:\tst_create_zip\tst_create_zip\tst_create_zip.obj
Error   13  error LNK2019: unresolved external symbol _zipOpen2_64 referenced in function _main C:\tst_create_zip\tst_create_zip\tst_create_zip.obj

I saw in zip.h that the functions are exported with extern "C" and the __cdecl calling convention, so the linker is expecting some kind of c-style name for the function (that always includes the underscore '_' ?)

Dependency walker for zlibwapi.dll (contains both zLib and Minilib) shows that the functions are exported without the underscore, like "zipClose", "_zipOpen2_64".

So the question is: how to tell the linker to search for the functions names without the underscore? Or should I use LoadLibrary() instead?

Thanks in advance.

Upvotes: 1

Views: 287

Answers (1)

Lelo
Lelo

Reputation: 912

It turns out I was missing a preprocessor directive

#ifdef ZLIB_WINAPI
#include <windows.h>
// No need for _export, use ZLIB.DEF instead. 
// For complete Windows compatibility, use WINAPI, not __stdcall. 
#define ZEXPORT WINAPI

When I defined ZLIB_WINAPI, it worked.

A thing that was misleading my comprehension was that there is no __declspec(dllexport) before the function declaration. Nevertheless, the linker is able to resolve the import.

Upvotes: 1

Related Questions