mingw编译lua-iconv库

⌚Time: 2025-07-27 16:06:00

👨‍💻Author: Jack Ge

lua5.1扫描win7目录得到的是gbk编码,需要使用iconv库转换。

从网上下载lua-conv库源码,准备编译成dll文件。地址是:https://github.com/lunarmodules/lua-iconv/tags

我下载了lua-iconv6。下载后解压。查看makefile文件,这是在linux系统下的makefile文件。

# luaiconv - Performs character set conversions in Lua
# (c) 2005-08 Alexandre Erwin Ittner <aittner@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# If you use this package in a product, an acknowledgment in the product
# documentation would be greatly appreciated (but it is not required).

#CC = gcc
#RM = rm

# Gives a nice speedup, but also spoils debugging on x86. Comment out this
# line when debugging.
OMIT_FRAME_POINTER = -fomit-frame-pointer

# Name of .pc file. "lua5.1" on Debian/Ubuntu
LUAPKG = lua5.1
CFLAGS = `pkg-config $(LUAPKG) --cflags` -fPIC -O3 -Wall
LFLAGS = -shared $(OMIT_FRAME_POINTER)
INSTALL_PATH = `pkg-config $(LUAPKG) --variable=INSTALL_CMOD`

## If your system doesn't have pkg-config, comment out the previous lines and
## uncomment and change the following ones according to your building
## enviroment.

#CFLAGS = -I/usr/include/lua5.1/ -fPIC -O3 -Wall
#LFLAGS = -shared $(OMIT_FRAME_POINTER)
#INSTALL_PATH = /usr/lib/lua/5.1


all: iconv.so

iconv.lo: luaiconv.c
    $(CC) -o iconv.lo -c $(CFLAGS) luaiconv.c

iconv.so: iconv.lo
    $(CC) -o iconv.so $(LFLAGS) $(LIBS) iconv.lo

install: iconv.so
    make test
    install -D -s iconv.so $(DESTDIR)/$(INSTALL_PATH)/iconv.so

clean:
    $(RM) iconv.so iconv.lo

test: iconv.so test_iconv.lua
    lua test_iconv.lua


这个makefile需要用到lua库文件,lua5.1.5库的下载地址是:https://sourceforge.net/projects/luabinaries/files/5.1.5/Windows%20Libraries/Static/

下载完成后解压。需要注意lua库的位数和mingw编译器的位数需要一致,比如都是32位。

之后就是把lua5.1库加入到makefile里面,然后把makefile转换成windows系统mingw格式的

# MinGW 编译配置
CC = gcc
RM = del /F /Q

# Lua 路径(根据你的实际路径修改)
LUA_INC = -ID:\BuildChanTools\lua-5.1.5_Win32_mingw4_lib\include
LUA_LIB = -LD:\BuildChanTools\lua-5.1.5_Win32_mingw4_lib

# 编译选项
CFLAGS = $(LUA_INC) -O2 -Wall
LFLAGS = -shared -s -static-libgcc
LIBS = $(LUA_LIB) -llua5.1

# 输出文件名(Windows 动态库后缀为 .dll)
TARGET = iconv.dll

all: $(TARGET)

iconv.o: luaiconv.c
    $(CC) -o iconv.o -c $(CFLAGS) luaiconv.c

$(TARGET): iconv.o
    $(CC) -o $(TARGET) $(LFLAGS) iconv.o $(LIBS)

clean:
    $(RM) $(TARGET) iconv.o

test: $(TARGET)
    lua test_iconv.lua

这个makefile文件需要加一个iconv库,因为这里还需要下载iconv库,如果不链接iconv库的话,我编译的时候就会出现问题

D:/BuildChanTools/i686-5.4.0-release-posix-dwarf-rt_v5-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/5.4.0/../../../../i686-w64-mingw32/lib/../lib/libiconv.a(localcharset.o):localcharset.c:(.text+0x7): undefined reference to `_imp__GetACP@0'

根据AI的提示需要链接参数-lkernel32,加了也没有用。但是我看到StackOverflow上面有人说需要链接iconv库。后来AI说这个lua-iconv依赖预先安装的iconv库。所以需要下载iconv库

下载地址是:https://gnuwin32.sourceforge.net/packages/libiconv.htm

我下载的是libiconv-1.9.2-1-lib.zip,解压。将库目录加入到makefile文件里之后就是最后的makefile文件

# MinGW 编译配置
CC = gcc
RM = del /F /Q

# Lua 路径(根据你的实际路径修改)
LUA_INC = -ID:\BuildChanTools\lua-5.1.5_Win32_mingw4_lib\include
LUA_LIB = -LD:\BuildChanTools\lua-5.1.5_Win32_mingw4_lib -LD:\BuildChanTools\libiconv-1.9.2-1-lib\lib

# 编译选项
CFLAGS = $(LUA_INC) -O2 -Wall
LFLAGS = -shared -s -static-libgcc
LIBS = $(LUA_LIB) -llua5.1 -liconv

# 输出文件名(Windows 动态库后缀为 .dll)
TARGET = iconv.dll

all: $(TARGET)

iconv.o: luaiconv.c
    $(CC) -o iconv.o -c $(CFLAGS) luaiconv.c

$(TARGET): iconv.o
    $(CC) -o $(TARGET) $(LFLAGS) iconv.o $(LIBS)

clean:
    $(RM) $(TARGET) iconv.o

test: $(TARGET)
    lua test_iconv.lua

之后在cmd里面打开lua-iconv源码目录,直接执行

mingw32-make

得到了输出

gcc -o iconv.o -c -ID:\BuildChanTools\lua-5.1.5_Win32_mingw4_lib\include -O2 -Wall luaiconv.c
gcc -o iconv.dll -shared -s -static-libgcc iconv.o -LD:\BuildChanTools\lua-5.1.5_Win32_mingw4_lib -LD:\BuildChanTools\libiconv-1.9.2-1-lib\lib -llua5.1 -liconv

编译就成功了,在目录下出现了iconv.dll文件,将这个dll文件加入到lua5.1的目录下就能够使用iconv模块了!