您的位置:首页 > 运维架构 > Linux

如何将Linux端POSIX标准的程序移植到windows端 (一)

2014-10-06 19:18 537 查看
由于项目需要,必须将Linux平台以POSIX标准改编的Lua解释器移植到windows平台下,并且需要在Qt中被多线程调用。

之前走了很多弯路,这里现将这些曲折描述下,不过这些方法对于Lua源码编译移植到window平台来说不失为一种更简单的方法,不过笔者将Lua的源码改了,所以在编译时遇到了很多困难。

1.下载Lua源码,并解压

Lua源码现在已将更新到lua-5.2.3了,由于笔者改编源码时用的是lua-5.1.5,所以这里下载lua-5.1.5.tar.gz。在windows下解压成lua-5.1.5文件夹,文件夹下有doc,etc,src,test文件夹,其中src文件夹下的就是lua的源码,在linux下在src下运行make命令就可以得到lua,luac解释器和编译器文件,不过在windows平台下不能获得。

2.下载Mingw,并安装

这里请参照/article/8245920.html,并记住一定要在Path路径中增加Mingw下bin目录所在路径。

3.在上述lua的解压文件夹lua-5.1.5下src目录下新建luacpath.h文件。

详细信息参照:https://gist.github.com/starwing/4756700。文件内容如下:

luacpath.h

#ifndef luacpath_h
#define luacpath_h

#undef LUA_CDIR
#undef LUA_CPATH_DEFAULT

#define LUA_CDIR	"!\\"
#define LUA_CPATH_DEFAULT \
LUA_CDIR"clibs\\?.dll;" LUA_CDIR"clibs\\loadall.dll;" \
LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll;" ".\\?.dll"

#endif /* luacpath_h */

4.在上述lua的解压文件夹lua-5.1.5下新建bin文件夹,在bin文件夹下新建build.bat批处理文件。文件内容如下:

build.bat

<pre name="code" class="html">@setlocal
@if "%1" == "mingw" goto :build_mgw
@if "%1" == "vs" goto :build_vs
@if "%1" == "dist" goto :install
@if "%1" == "" goto :build_mgw

@echo usage: %1 [mingw|vs|dist]
@goto :EOF

:build_mgw
@set srcdir=..\src\
@set CFLAGS=-s -O3 -Wall -I. "-DLUA_USER_H=\"luacpath.h\""

@echo build lua52.dll ...
gcc %CFLAGS% -DLUA_BUILD_AS_DLL -c %srcdir%*.c
@del lua.o luac.o
gcc -s -mdll -o lua52.dll -Wl,--out-implib,liblua52.dll.a *.o
@echo build lua.exe ...
gcc %CFLAGS% -o lua.exe %srcdir%lua.c -L. -llua52
@echo build luac.exe ...
gcc %CFLAGS% -c %srcdir%*.c
@del lua.o luac.o
ar rcs liblua52.a *.o
gcc %CFLAGS% -o luac.exe %srcdir%luac.c -L. -static -llua52
@del *.o 2>nul
goto :dist

:build_vs
@set srcdir=..\src\
@set MYCOMPILE=/nologo /MD /O2 /W3 /c /D_CRT_SECURE_NO_DEPRECATE "/DLUA_USER_H=\"<span style="font-family: Arial, Helvetica, sans-serif;">luacpath.h\"</span>"
@set MYLINK=link /nologo
@set MYLIB=lib /nologo
@set MYMT=mt /nologo

@echo build lua52.dll ...
%MYCOMPILE% /DLUA_BUILD_AS_DLL %srcdir%*.c
@del lua.obj luac.obj
%MYLINK% /DLL /out:lua52.dll *.obj
if exist lua52.dll.manifest^
%MYMT% -manifest lua52.dll.manifest -outputresource:lua52.dll;2
%MYCOMPILE% /DLUA_BUILD_AS_DLL %srcdir%lua.c
%MYLINK% /out:lua.exe lua.obj lua52.lib
if exist lua.exe.manifest^
%MYMT% -manifest lua.exe.manifest -outputresource:lua.exe
%MYCOMPILE% %srcdir%*.c
del lua.obj luac.obj
%MYLIB% /out:lua52s.lib *.obj
%MYCOMPILE% %srcdir%luac.c
%MYLINK% /out:luac.exe luac.obj lua52s.lib
if exist luac.exe.manifest^
%MYMT% -manifest luac.exe.manifest -outputresource:luac.exe
@del *.exp *.obj *.manifest
goto :dist

:dist
@echo install ...
@set dstdir=.\Lua52\
mkdir %dstdir%         2>nul
mkdir %dstdir%clibs    2>nul
mkdir %dstdir%include  2>nul
mkdir %dstdir%lib      2>nul
mkdir %dstdir%lua      2>nul

copy /y *.dll            %dstdir%           >nul
copy /y *.exe            %dstdir%           >nul
copy /y *.a              %dstdir%lib        >nul
copy /y %srcdir%lua*.h*  %dstdir%include    >nul
copy /y %srcdir%laux*.h  %dstdir%include    >nul
copy /y %dstdir%lua.exe  %dstdir%lua52.exe  >nul
copy /y %dstdir%luac.exe %dstdir%lua52c.exe >nul

:EOF
echo finish



5.双击build.bat即可生成lua.exe和luac.exe文件。若想知道编译的详细信息,可以在cmd下跳转到上述bin目录下运行build.bat

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: