スペース
スペース

Reputation: 21

How do I compile with zlib on windows using gcc?

My aim is to compile zlib alongside my application files, I use gcc from tdm-gcc and have cloned the repository https://github.com/madler/zlib

in /example there is a zpipe.c file which utilizes the library but I can't seem to get it compiling on windows.

I've added -lz to the end of my compile command and I receive /x86_64-w64-mingw32/bin/ld.exe: cannot find -lz error.

If I try to compile with just -Izlib I end up with these errors:

$ gcc -Izlib zpipe.c -o zpipe
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x6a): undefined reference to `deflateInit_'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0xca): undefined reference to `deflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x12e): undefined reference to `deflate'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x1be): undefined reference to `deflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x23c): undefined reference to `deflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x2c0): undefined reference to `inflateInit_'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x320): undefined reference to `inflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x36f): undefined reference to `inflate'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x3d1): undefined reference to `inflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x440): undefined reference to `inflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x474): undefined reference to `inflateEnd'
collect2.exe: error: ld returned 1 exit status

Any help is greatly appreciated! I've searched stack overflow and tried all the suggestions and also googled and I'm still having issues.

The errors seem trivial and I've fixed such errors in the past, but this one I'm stuck on and I'm not sure what else to try.

Upvotes: 1

Views: 3600

Answers (2)

Brecht Sanders
Brecht Sanders

Reputation: 7305

When using MSYS2 shell it's quite simple, just run the following commands (replace /usr/local with the desired location):

INSTALLPREFIX=/usr/local
wget -c http://www.zlib.net/zlib-1.2.12.tar.gz
tar xfz zlib-1.2.12.tar.gz
cd zlib-1.2.12
make -f win32/Makefile.gcc install \
 SHARED_MODE=1 \
 INCLUDE_PATH=$INSTALLPREFIX/include \
 LIBRARY_PATH=$INSTALLPREFIX/lib \
 BINARY_PATH=$INSTALLPREFIX/bin 

Upvotes: 1

スペース
スペース

Reputation: 21

1) Creating the static library.

Inside the zlib folder where the .c and .h files are located running gcc -c *.c will generate .o files that can be used to build the library.

after you have the .o files, running ar -rc libz.a *.o will generate a libz.a file, this will allow you to link via -lz

2) Compiling zpipe.c into zpipe.exe

put zpipe.c into the folder where libz.a is located, this is for simplicity when compiling.

running gcc -L. zpipe.c -o zpipe.exe -lz will create zpipe.exe.

3) Quick demo

Create a file hello.txt with Hello World! inside.

Run ./zpipe.exe < hello.txt > output.txt this will inflate the data in hello.txt and put it in output.txt

Run ./zpipe.exe -d < output.txt > original.txt to decompress the file and you will see Hello World! inside original.txt

Definitions

-L. tells gcc to use the current folder for the header files.

-lz tells gcc to compile with a static library who's name starts with lib and ends in .a for eg. libexample.a would be -lexample

Upvotes: 1

Related Questions