Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static ZLIB (1.2.8) linking on Visual Studio 2012

I can't, for the love of God, to static link the ZLIB libs. I have been struggling for a couple hours now with no success. Well, I have followed this tutorial and successfuly compiled both zlibstat.lib and zlibwapi.lib for 32 bits. After setting up my project to use the ZLIB folder with the libraries (Linker > General > Additional Library Directories) and setting zlibwapi.lib (only) as an dependency (Linker > Input > Additional Dependencies) I got it to work, however, that was a dynamic link (I need to distribute my application with the ZLIB dll). I usually use dynamic linking on Debug and static on Release.

I have tried looking for what the hell is the zlibstat.lib and whats it used for, if not for static linking, assuming the "stat" suffix.

Are there any preprocessor to be added into my project, something like ZLIB_STATIC or something, to use static linking of ZLIB or should I have never removed the ZLIB_WINAPI from the zlibstat project, just like the above link told me to do? Is it impossible to static link ZLIB (then, whats zlibstat.lib for?)?

I am pretty lost. Any help is GREATLY appreciated.

Edit (Extra information):

Errors:

error LNK2001: unresolved external symbol _inflateInit_@12
error LNK2001: unresolved external symbol _inflate@8
error LNK2001: unresolved external symbol _inflateEnd@4

Linking:

Unlike the dynamic link (that worked) where I added zlibwapi.lib as a dependency, for the static linking I'm trying to achieve I added zlibstat.lib as a dependency instead! No other libs were added!

This question might look like this (kind of).

like image 668
Henri Avatar asked Oct 28 '15 21:10

Henri


People also ask

How do I add zlib to Visual Studio?

zip file into "C:\Program Files\Zlib" . After that, I have opened visual studio 2017 and goes to property => C/C++ => general => additional include directories and added that path : "C:\Program Files\Zlib\msvc2015_64" . After that, I have added #include <zlib. h> header file in my project.

How to compile static library of zlib using Visual Studio?

Compile Static Library of zlib Using Visual Studio Downloadand extract zlib Launch VS commad prompt: Microsoft Visual Studio 2008 -> Visual Studio Tools -> Visual Studio 2008 Command Prompt Build: $ cd zlib-1.2.8 $ nmake /f win32/Makefile.msc

What is the difference between zdll and zlib?

zlib.lib <- our static library. yeah! zdll.lib <- for dynamic linking, not using for now zlib1.dll <- for dynamic linking, not using for now Copy zlib.liband zlib.hto your project. Done! Newer posts Older posts

How to configure static linking in Visual Studio?

How to configure Static Linking in Visual Studio? Right click on your application (not solution) in the solution explorer and select properties. Select eighter 'Multi-threaded' or 'Multi-threaded debug' depending on what you want in stead of 'Multi-threaded DLL' or 'Multi-threaded debug DLL'

How do I link zlibvc to a VBA project?

Right-click the zlibvc project and open its property pages: Give it a go. For the Win64 version, the linker switch does not apply, just go directly to the build process. Note that you’ll end up with a zlibwapi.dll file that you can use with your vb/vba projects.


1 Answers

I have finally managed to solve my problem. For those that end up in such a problem, heres how to solve it:

If you followed the tutorial in my first post, you would have removed ZLIB_WINAPI from the zlibstat project's Preprocessor. However, after setting up my own project (setting ZLIB dependencies path, LIB dependencies libraries etc) that uses ZLIB, I 'accidently' included/defined the damned ZLIB_WINAPI macro in the header file I'm using ZLIB, right before including "zlib.h".

One curious thing about this is that when launching the app on debug mode (which was using dynamic linking), everything succeeded and worked like a charm, without any warnings or whatsoever, HOWEVER, on release mode (which was using the static linking), it crashed.

So, to clear things up, the tutorial tells us to remove the ZLIB_WINAPI preprocessor from the zlibstat project, which produces the static lib, whilst the zlibvc project has the ZLIB_WINAPI in its preprocessor. In other words, that means that if we're using different linkings for each configuration (debug/release), we have to add the ZLIB_WINAPI macro or not!

Define the ZLIB_WINAPI macro before including "zlib.h" if youre using dynamic linking (zlibwapi.lib) and that the zlibvc project remains unmodified (assuming you correctly followed the tutorial from the link above) and do not define it if you removed ZLIB_WINAPI from the zlibstat project (like the tutorial tell us to)!

One helpful macro I used on my own project is as follows:

// Since we used dynamic linking for debug, we have to define the ZLIB_WINAPI
#if defined(_WIN32) && defined(_DEBUG)
    #define ZLIB_WINAPI
#endif
#include <zlib.h>

Things got really confusing and I really hope I was clear enough.

like image 174
Henri Avatar answered Oct 02 '22 13:10

Henri