lua 编译

来源:互联网 发布:java命令行执行junit 编辑:程序博客网 时间:2024/05/17 08:02

lua 编译脚本

luavs.bat

@rem Script to build Lua under "Visual Studio .NET Command Prompt".@rem Do not run from this directory; run it from the toplevel: etc\luavs.bat .@rem It creates lua5.3.2.dll, lua5.3.2.lib, lua.exe, and luac.exe in src.@rem (contributed by David Manura and Mike Pall)@echo off@setlocal@set MYCOMPILE=cl /nologo /MD /O2 /W3 /c /D_CRT_SECURE_NO_DEPRECATE@set MYLINK=link /nologo@set MYMT=mt /nologocd src%MYCOMPILE% /DLUA_BUILD_AS_DLL l*.cdel lua.obj luac.obj%MYLINK% /DLL /out:lua5.3.2.dll l*.objif exist lua5.3.2.dll.manifest^  %MYMT% -manifest lua5.3.2.dll.manifest -outputresource:lua5.3.2.dll;2%MYCOMPILE% /DLUA_BUILD_AS_DLL lua.c%MYLINK% /out:lua.exe lua.obj lua5.3.2.libif exist lua.exe.manifest^  %MYMT% -manifest lua.exe.manifest -outputresource:lua.exe%MYCOMPILE% l*.c %print.c%del lua.obj linit.obj lbaselib.obj ldblib.obj liolib.obj lmathlib.obj^    loslib.obj ltablib.obj lstrlib.obj loadlib.obj%MYLINK% /out:luac.exe *.objif exist luac.exe.manifest^  %MYMT% -manifest luac.exe.manifest -outputresource:luac.exedel *.obj *.manifestcd ..

lua 编译之后创建一个控制台程序编译出错:

1>------ 已启动生成: 项目: ConsoleApplication2, 配置: Release Win32 ------1>  ConsoleApplication2.cpp1>ConsoleApplication2.obj : error LNK2001: 无法解析的外部符号 "struct lua_State * __cdecl luaL_newstate(void)" (?luaL_newstate@@YAPAUlua_State@@XZ)1>ConsoleApplication2.obj : error LNK2001: 无法解析的外部符号 "void __cdecl luaL_openlibs(struct lua_State *)" (?luaL_openlibs@@YAXPAUlua_State@@@Z)1>ConsoleApplication2.obj : error LNK2001: 无法解析的外部符号 "int __cdecl luaL_loadfilex(struct lua_State *,char const *,char const *)" (?luaL_loadfilex@@YAHPAUlua_State@@PBD1@Z)1>ConsoleApplication2.obj : error LNK2001: 无法解析的外部符号 "int __cdecl lua_pcallk(struct lua_State *,int,int,int,int,int (__cdecl*)(struct lua_State *,int,int))" (?lua_pcallk@@YAHPAUlua_State@@HHHHP6AH0HH@Z@Z)1>ConsoleApplication2.obj : error LNK2001: 无法解析的外部符号 "void __cdecl lua_close(struct lua_State *)" (?lua_close@@YAXPAUlua_State@@@Z)1>C:\Users\Administrator\Documents\Visual Studio 2012\Projects\ConsoleApplication2\Release\ConsoleApplication2.exe : fatal error LNK1120: 5 个无法解析的外部命令========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

原因:C++项目,引用的lua头文件必须这样写:

extern "C" {  #include <lualib.h>  #include <lauxlib.h>  } 

附带test.lua:

function show()      local b = {}      local index      for index = 1,10,1 do          print(index)      end  end  show()
@rem Script to build Lua under "Visual Studio .NET Command Prompt".@rem Do not run from this directory; run it from the toplevel: etc\luavs.bat .@rem It creates lua%LUA_VERSION%.dll, lua%LUA_VERSION%.lib, lua.exe, and luac.exe in src.@rem (contributed by David Manura and Mike Pall)@set LUA_VERSION=5.3.3@echo off@setlocal@set MYCOMPILE=cl /nologo /MD /O2 /W3 /c /D_CRT_SECURE_NO_DEPRECATE@set MYLINK=link /nologo@set MYMT=mt /nologocd src%MYCOMPILE% /DLUA_BUILD_AS_DLL l*.cdel lua.obj luac.obj%MYLINK% /DLL /out:lua%LUA_VERSION%.dll l*.objif exist lua%LUA_VERSION%.dll.manifest^  %MYMT% -manifest lua%LUA_VERSION%.dll.manifest -outputresource:lua%LUA_VERSION%.dll;2%MYCOMPILE% /DLUA_BUILD_AS_DLL lua.c%MYLINK% /out:lua.exe lua.obj lua%LUA_VERSION%.libif exist lua.exe.manifest^  %MYMT% -manifest lua.exe.manifest -outputresource:lua.exe%MYCOMPILE% l*.c %print.c%del lua.obj linit.obj lbaselib.obj ldblib.obj liolib.obj lmathlib.obj^    loslib.obj ltablib.obj lstrlib.obj loadlib.obj%MYLINK% /out:luac.exe *.objif exist luac.exe.manifest^  %MYMT% -manifest luac.exe.manifest -outputresource:luac.exedel *.obj *.manifestcd ..

cmake版本

./CMakeLists.txt

cmake_minimum_required(VERSION 3.5)PROJECT(LUA)SET(LUA_MAJOR_VERSION 5)SET(LUA_MINOR_VERSION 3)SET(LUA_PATCH_VERSION 3)ADD_SUBDIRECTORY(src)

./src/CMakeLists.txt

SET(LUA_LIB_NAME lua${LUA_MAJOR_VERSION}.${LUA_MINOR_VERSION}.${LUA_PATCH_VERSION})SET(LUA_NAME lua)SET(LUAC_NAME luac)set(LUA_LIB_SRC     lapi.c    lauxlib.c    lbaselib.c    lbitlib.c    lcode.c    lcorolib.c    lctype.c    ldblib.c    ldebug.c    ldo.c    ldump.c    lfunc.c    lgc.c    linit.c    liolib.c    llex.c    lmathlib.c    lmem.c    loadlib.c    lobject.c    lopcodes.c    loslib.c    lparser.c    lstate.c    lstring.c    lstrlib.c    ltable.c    ltablib.c    ltm.c    lundump.c    lutf8lib.c    lvm.c    lzio.c    )set(LUAC_SRC     lapi.c    lauxlib.c    lbitlib.c    lcode.c    lcorolib.c    lctype.c    ldebug.c    ldo.c    ldump.c    lfunc.c    lgc.c    llex.c    lmem.c    lobject.c    lopcodes.c    lparser.c    lstate.c    lstring.c    ltable.c    ltm.c    luac.c    lundump.c    lutf8lib.c    lvm.c    lzio.c    )SET(LUA_SRC ${LUA_LIB_SRC} lua.c)MESSAGE(STATUS ${LUA_LIB_SRC})MESSAGE(STATUS ${LUA_SRC})MESSAGE(STATUS ${LUAC_SRC})ADD_DEFINITIONS("-DLUA_BUILD_AS_DLL")ADD_LIBRARY(${LUA_LIB_NAME} SHARED ${LUA_LIB_SRC})ADD_EXECUTABLE(${LUA_NAME} lua.c)TARGET_LINK_LIBRARIES(${LUA_NAME} ${LUA_LIB_NAME})ADD_EXECUTABLE(${LUAC_NAME} ${LUAC_SRC})
0 0
原创粉丝点击