How to compile static libpng with static zlib [exe linker errors]?

I'm at my wit's end trying to get this to work. I've been struggling for hours on end with CMake trying (and retrying) to do the following:

(1) compile zlib as static
(2) compile libpng as static with the static zlib I just got
(3) use this conglomerated library in an executable program

The result of this is always some variation of:

1C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngread.obj):pngread.c|| undefined reference to `_imp__inflateEnd'| 2C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngread.obj):pngread.c|| undefined reference to `_imp__inflate'| 3C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngread.obj):pngread.c|| undefined reference to `_imp__inflateInit_'| 4C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngread.obj):pngread.c|| undefined reference to `_imp__inflateInit_'| 5C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(png.obj):png.c|| undefined reference to `_imp__inflateReset'| 6C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(png.obj):png.c|| undefined reference to `_imp__crc32'| 7C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(png.obj):png.c|| undefined reference to `_imp__crc32'| 8C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngwrite.obj):pngwrite.c|| undefined reference to `_imp__deflateEnd'| 9C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngwrite.obj):pngwrite.c|| undefined reference to `_imp__deflate'| 10C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngrutil.obj):pngrutil.c|| undefined reference to `_imp__inflate'| 11C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngrutil.obj):pngrutil.c|| undefined reference to `_imp__inflateReset'| 12C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngrutil.obj):pngrutil.c|| undefined reference to `_imp__inflate'| 13C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngrutil.obj):pngrutil.c|| undefined reference to `_imp__inflateReset'| 14C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngwutil.obj):pngwutil.c|| undefined reference to `_imp__deflate'| 15C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngwutil.obj):pngwutil.c|| undefined reference to `_imp__deflate'| 16C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngwutil.obj):pngwutil.c|| undefined reference to `_imp__deflateReset'| 17C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngwutil.obj):pngwutil.c|| undefined reference to `_imp__deflate'| 18C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngwutil.obj):pngwutil.c|| undefined reference to `_imp__deflateReset'| 19C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngwutil.obj):pngwutil.c|| undefined reference to `_imp__deflate'| 20C:\Program Files\CodeBlocks\MinGW\lib\libpng12_static.a(pngwutil.obj):pngwutil.c|| undefined reference to `_imp__deflateInit2_'| 21||=== Build finished: 20 errors, 0 warnings ===|

or sometimes without the prefixes:
undefined reference to `inflateEnd'

I know these functions are from zlib. Linking the executable with the static zlib does not help, no matter what order the libraries are in. :-/

If it matters any, I was able to statically compile/link Allegro 4.4.2, but I can't get these specific libraries to cooperate.

What am I doing wrong? I would appreciate step-by-step instructions (if it's necessary).

Trent Gamblin

I don't understand what you're trying to do here. You shouldn't link one static library to another. You should build both independantly (you'll need zlib headers to compile libpng though) and then link both libraries to your program.

edit: if those errors are actually from your program build, make sure libpng is linked ahead in order of zlib.


Well I was trying to compile both into one library, so that isn't possible? OK, kinda makes sense. :P

I'm still linking to both libraries for the EXE, and it's still giving me those undefined reference errors (even did a full rebuild).

Going to delete all related the headers and lib files I have and try this once more from scratch...

Trent Gamblin

Did you link them in the right order?


I tried both ways last time.

This time, when compiling libpng the shared version of the library stops on undefined reference errors similar to what is found in the executable. Do I have to specify the dynamic version of zlib in the configure script (using CMake)?

Trent Gamblin

Where does cmake come into play here? Are you building the library with examples (which have 'main' functions) built into them? What are you using to build libpng, its own build system or your own? Paste the complete output of the build here please.

edit: if it's the libpng examples/tests failing then your zlib isn't built right. It doesn't matter if it's static or dynamic.


I've been using CMake to configure, both zlib and libpng. I'm compiling with mingw32-make.

I've re-ran the zlib build with shared lib built (now I have both). Output:

1C:\zlib\build>mingw32-make 2[ 5%] Building C object CMakeFiles/zlib.dir/adler32.obj 3[ 10%] Building C object CMakeFiles/zlib.dir/compress.obj 4[ 15%] Building C object CMakeFiles/zlib.dir/crc32.obj 5[ 20%] Building C object CMakeFiles/zlib.dir/deflate.obj 6[ 25%] Building C object CMakeFiles/zlib.dir/gzclose.obj 7[ 30%] Building C object CMakeFiles/zlib.dir/gzlib.obj 8C:\zlib\gzlib.c: In function 'gz_error': 9C:\zlib\gzlib.c:518: warning: visibility attribute not supported in this configu 10ration; ignored 11[ 35%] Building C object CMakeFiles/zlib.dir/gzread.obj 12[ 40%] Building C object CMakeFiles/zlib.dir/gzwrite.obj 13[ 45%] Building C object CMakeFiles/zlib.dir/inflate.obj 14[ 50%] Building C object CMakeFiles/zlib.dir/infback.obj 15[ 55%] Building C object CMakeFiles/zlib.dir/inftrees.obj 16C:\zlib\inftrees.c: In function 'inflate_table': 17C:\zlib\inftrees.c:330: warning: visibility attribute not supported in this conf 18iguration; ignored 19[ 60%] Building C object CMakeFiles/zlib.dir/inffast.obj 20C:\zlib\inffast.c: In function 'inflate_fast': 21C:\zlib\inffast.c:324: warning: visibility attribute not supported in this confi 22guration; ignored 23[ 65%] Building C object CMakeFiles/zlib.dir/trees.obj 24C:\zlib\trees.c: In function '_tr_init': 25C:\zlib\trees.c:410: warning: visibility attribute not supported in this configu 26ration; ignored 27C:\zlib\trees.c: In function '_tr_stored_block': 28C:\zlib\trees.c:883: warning: visibility attribute not supported in this configu 29ration; ignored 30C:\zlib\trees.c: In function '_tr_align': 31C:\zlib\trees.c:919: warning: visibility attribute not supported in this configu 32ration; ignored 33C:\zlib\trees.c: In function '_tr_tally': 34C:\zlib\trees.c:1071: warning: visibility attribute not supported in this config 35uration; ignored 36C:\zlib\trees.c: In function '_tr_flush_block': 37C:\zlib\trees.c:1020: warning: visibility attribute not supported in this config 38uration; ignored 39C:\zlib\trees.c: At top level: 40C:\zlib\trees.c:1244: warning: visibility attribute not supported in this config 41uration; ignored 42C:\zlib\trees.c:1244: warning: visibility attribute not supported in this config 43uration; ignored 44[ 70%] Building C object CMakeFiles/zlib.dir/uncompr.obj 45[ 75%] Building C object CMakeFiles/zlib.dir/zutil.obj 46C:\zlib\zutil.c: In function 'zcfree': 47C:\zlib\zutil.c:316: warning: visibility attribute not supported in this configu 48ration; ignored 49C:\zlib\zutil.c: In function 'zcalloc': 50C:\zlib\zutil.c:308: warning: visibility attribute not supported in this configu 51ration; ignored 52Linking C shared library libzlib1.dll 53Creating library file: libzlib.dll.a 54[ 80%] Built target zlib 55Linking C executable example.exe 56[ 85%] Built target example 57Linking C executable example64.exe 58[ 90%] Built target example64 59Linking C executable minigzip.exe 60[ 95%] Built target minigzip 61Linking C executable minigzip64.exe 62[100%] Built target minigzip64 63 64C:\zlib\build>mingw32-make install 65[ 80%] Built target zlib 66[ 85%] Built target example 67[ 90%] Built target example64 68[ 95%] Built target minigzip 69[100%] Built target minigzip64 70Install the project... 71-- Install configuration: "MinSizeRel" 72-- Installing: C:/MinGW/lib/libzlib.dll.a 73-- Installing: C:/MinGW/bin/libzlib1.dll 74-- Up-to-date: C:/MinGW/include/zconf.h 75-- Up-to-date: C:/MinGW/include/zlib.h 76-- Up-to-date: C:/MinGW/share/man/man3/zlib.3 77 78C:\zlib\build>

Do those warnings mean the library is incorrectly built?

Here's libpng compile process:

1C:\libpng\build>mingw32-make 2Scanning dependencies of target png12 3[ 3%] Building C object CMakeFiles/png12.dir/png.obj 4[ 6%] Building C object CMakeFiles/png12.dir/pngerror.obj 5[ 9%] Building C object CMakeFiles/png12.dir/pngget.obj 6[ 12%] Building C object CMakeFiles/png12.dir/pngmem.obj 7[ 16%] Building C object CMakeFiles/png12.dir/pngpread.obj 8[ 19%] Building C object CMakeFiles/png12.dir/pngread.obj 9[ 22%] Building C object CMakeFiles/png12.dir/pngrio.obj 10[ 25%] Building C object CMakeFiles/png12.dir/pngrtran.obj 11[ 29%] Building C object CMakeFiles/png12.dir/pngrutil.obj 12[ 32%] Building C object CMakeFiles/png12.dir/pngset.obj 13[ 35%] Building C object CMakeFiles/png12.dir/pngtrans.obj 14[ 38%] Building C object CMakeFiles/png12.dir/pngwio.obj 15[ 41%] Building C object CMakeFiles/png12.dir/pngwrite.obj 16[ 45%] Building C object CMakeFiles/png12.dir/pngwtran.obj 17[ 48%] Building C object CMakeFiles/png12.dir/pngwutil.obj 18Linking C shared library libpng12.dll 19Creating library file: libpng12.dll.a 20[ 48%] Built target png12 21Scanning dependencies of target png12_static 22[ 51%] Building C object CMakeFiles/png12_static.dir/png.obj 23[ 54%] Building C object CMakeFiles/png12_static.dir/pngerror.obj 24[ 58%] Building C object CMakeFiles/png12_static.dir/pngget.obj 25[ 61%] Building C object CMakeFiles/png12_static.dir/pngmem.obj 26[ 64%] Building C object CMakeFiles/png12_static.dir/pngpread.obj 27[ 67%] Building C object CMakeFiles/png12_static.dir/pngread.obj 28[ 70%] Building C object CMakeFiles/png12_static.dir/pngrio.obj 29[ 74%] Building C object CMakeFiles/png12_static.dir/pngrtran.obj 30[ 77%] Building C object CMakeFiles/png12_static.dir/pngrutil.obj 31[ 80%] Building C object CMakeFiles/png12_static.dir/pngset.obj 32[ 83%] Building C object CMakeFiles/png12_static.dir/pngtrans.obj 33[ 87%] Building C object CMakeFiles/png12_static.dir/pngwio.obj 34[ 90%] Building C object CMakeFiles/png12_static.dir/pngwrite.obj 35[ 93%] Building C object CMakeFiles/png12_static.dir/pngwtran.obj 36[ 96%] Building C object CMakeFiles/png12_static.dir/pngwutil.obj 37Linking C static library libpng12_static.a 38[ 96%] Built target png12_static 39Scanning dependencies of target pngtest 40[100%] Building C object CMakeFiles/pngtest.dir/pngtest.obj 41C:\libpng\pngtest.c: In function 'pngtest_write_data': 42C:\libpng\pngtest.c:400: warning: 'io_ptr' is deprecated (declared at C:\libpng\ 43png.h:1254) 44Linking C executable pngtest.exe 45CMakeFiles/pngtest.dir/pngtest.obj:pngtest.c:(.text+0x11fc): undefined reference 46 to `png_libpng_ver' 47CMakeFiles/pngtest.dir/pngtest.obj:pngtest.c:(.text+0x1237): undefined reference 48 to `png_libpng_ver' 49collect2: ld returned 1 exit status 50mingw32-make[2]: *** [pngtest.exe] Error 1 51mingw32-make[1]: *** [CMakeFiles/pngtest.dir/all] Error 2 52mingw32-make: *** [all] Error 2 53 54C:\libpng\build>

Trent Gamblin

Is this a developement version of libpng? Its failing to compile its own tests. In any case, they both seem to have built correctly. The only thing to do is try using them and see if they work.


So far, nothing. I tried using these two compiles and I STILL get undefined's.

I might have the wrong ZIP of the source code, is the only conclusion I can reasonably come to.

I'll edit back in here if I get it to work off of a different source version/zip/whatever. It'll be a while before I can try though, I have other things to do now ("maintenance" on a new laptop).

EDIT: Yep, that was the source of all this mess, I was using the wrong zlib source code distribution. Problem SOLVED. ;D

Thank you, Trent, for the help. :)

Edgar Reynaldo

To build zlib you can use win32/makefile.gcc or else you need MSYS. I don't know why you're trying to use cmake to build zlib, because it doesn't come with CMakeLists.txt unless that's a recent addition.

With MinGW

cd zlib
copy win32\makefile.gcc .\makefile.gcc
mingw32-make -fmakefile.gcc
mingw32-make install prefix=c:\mingw -fmakefile.gcc


cd zlib
mingw32-make install prefix=/c/mingw

To build libpng, it's pretty much the same except you can use a custom makefile, MSYS, or Cmake.


cd png
copy scripts\makefile.mingw .\makefile.mingw
mingw32-make -fmakefile.mingw
mingw32-make install -fmakefile.mingw


cd png
./configure --prefix=/c/mingw
mingw32-make install


cd png
mkdir build
cd build
cmake -G "MinGW Makefiles" ..
mingw32-make install

When building with MinGW or MSYS by themselves, you should get both the static and dynamic libraries by default.

Too late to the party...

Thread #608407. Printed from