1. 程式人生 > >如何將Linux端POSIX標準的程式移植到windows端 (一)

如何將Linux端POSIX標準的程式移植到windows端 (一)

由於專案需要,必須將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,並安裝

3.在上述lua的解壓資料夾lua-5.1.5下src目錄下新建luacpath.h檔案。

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