PiFace
PiFace

Reputation: 621

How to install lua-zlib with LuaRocks on Windows?

I've written a program with Lua, in a Linux environment, and it uses the Lua module ZipWriter and its dependecies (lua-zlib and struct). I'm trying to release to Windows as well, but I'm having trouble building lua-zlib.

I'm using LuaRocks to install all other packages, with standard commands. So, to install lua-zlib, I just used > luarocks install lua-zlib, but of course it wouldn't work, as zlib itself wasn't installed, and lua-zlib is a binding to that library.

Installing https://luarocks.org/lua-zlib-1.2-0.src.rock

Error: Could not find header file for ZLIB
  No file zlib.h in c:/external/include
  No file zlib.h in c:/mingw/include
  No file zlib.h in c:/windows/system32/include
You may have to install ZLIB in your system and/or pass ZLIB_DIR or ZLIB_INCDIR to the luarocks command.
Example: luarocks install lua-zlib ZLIB_DIR=/usr/local

So, I found a link in that page for different downloads of zlib for Windows. I downloaded both the "Complete package, except sources" and "Sources" Setups, installed them, and they created folders and files under the directory C:\Program Files (x86)\GnuWin32, all related to zlib. I followed the example provided by that error log and tried running luarocks again:

> luarocks install lua-zlib ZLIB_DIR="C:\Program Files (x86)\GnuWin32"

But again, another error:

Installing https://luarocks.org/lua-zlib-1.2-0.src.rock

mingw32-gcc -O2 -c -o lua_zlib.o -IC:\lua\luajit lua_zlib.c -DLZLIB_COMPAT -IC:\Program Files (x86)\GnuWin32/include
mingw32-gcc -shared -o zlib.dll lua_zlib.o -lC:\Program Files (x86)\GnuWin32/zlib C:\lua\luajit/lua51.dll -lMSVCRT
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lC:\Program Files (x86)\GnuWin32/zlib
collect2.exe: error: ld returned 1 exit status

Error: Build error: Failed compiling module zlib.dll

And indeed, there was no file/directory in C:\Program Files (x86)\GnuWin32/zlib, as the error shows. For some reason, that was not installed. What am I missing?

Note: as the error log shows, I have mingw32-gcc as the compiler, in case this is useful.

Upvotes: 3

Views: 1720

Answers (2)

Orachigami
Orachigami

Reputation: 716

2024 Manual

Installation guide from scratch

In this manual I will describe the way to install ZipWriter from the first to the last step. It will include lua + luarocks download and screenshots. ZipWriter supports lzlib rock which doesn't require manual library compilation.

You will need Git Bash (installed with Git automatically).

This guide doesn't work on PowerShell.

Make sure you install 32bit version.

Preparations (skip if it is the first time you install Lua)

  1. Project directory clear up
    • Delete lua_modules if exists
    • Delete .luarocks if exists
    • Delete .luarocks in %UserProfile% dir if exists
    • Make sure that .gitignore file ends with new empty line
  2. Remove all related directories from PATH variable

Download

  1. Create path for lua and luarocks binaries: C:\lua (to suit this guide)
  2. Download lua-5.3.x_Win32_bin.zip and lua-5.3.x_Sources.zip where x is the latest patch version from https://luabinaries.sourceforge.net/download.html
  3. Download i686-14.2.0-release-win32-dwarf-msvcrt-rt_v12-rev0.7z for dependencies compilation from https://github.com/niXman/mingw-builds-binaries/releases (link source https://www.mingw-w64.org/downloads/)
  4. Download luarocks 32 bit: https://luarocks.org/releases/luarocks-3.11.1-windows-32.zip (source: https://github.com/luarocks/luarocks/wiki/Download)
  5. Unpack everything to see the following result

C:/lua directory structure

Configuration

  1. Add following directories to the PATH variable:
C:\lua
C:\lua\mingw32\bin
C:\lua\mingw32\i686-w64-mingw32\bin

PATH variable

  1. Open Git Bash in the project directory
  2. Execute following commands:
luarocks config lua_version 5.3
luarocks config variables.LUA_INCDIR "C:\lua\lua53\include"
luarocks init
luarocks install lzlib ZLIB_DIR="C:\lua\mingw32\i686-w64-mingw32"
luarocks install zipwriter

Execution results

Further usage:

./lua.bat your_script.lua

Upvotes: 1

wp78de
wp78de

Reputation: 19000

It should work using a static zlib instead. To do so, you can follow this guide on Github. Basically, you need

  1. Functional zlib library installed
    Download from https://zlib.net , use cmake to generate Visual Studio solution (e.g. in c:\lib\zlib)

    cmake .. -DCMAKE_INSTALL_PREFIX=c:\lib\zlib
    

    then build the INSTALL project from the resulting solution, using 'Release' build type in VS.

  2. Download luarock lua-zlib:

    mkdir c:\lib\lua-zlib
    c:
    cd \lib\lua-zlib
    luarocks download lua-zlib
    
  3. Edit your lua-zlib*.rockspec file (e.g. in c:\lib\lua-zlib), add , "ZLIB_STATIC" to build.modules.zlib.defines, change platform.windows.modules.zlib.libraries from "$(ZLIB_LIBDIR)/zlib" to "$(ZLIB_LIBDIR)/zlibstatic"

  4. Install the luarock from local source (change the filename to match the existing one):

    cd c:\lib\lua-zlib 
    luarocks install lua-zlib*.rockspec
    

Upvotes: 1

Related Questions