本文主要通过Premake4生成编译工程来于快速编译出 C与lua交互 使用的库和可执行文件。
工具: Premake4
开源代码: Lua-5.2.4 src目录下所有文件
本专题【C与lua交互】中使用的代码:myclua.c , c_lib1.c , c_lib2.c
Premake4 生成编译工程的配置文件如下:
-- premake4.lua
-- >premake4 --file=premake4.lua --os=windows vs2012
-- >premake4 --file=premake4.lua --os=linux --platform=x64 gmake
-- > devenv c_lib_for_lua.sln /build "Release|Win32"
solution 'c_lib_for_lua'
configurations {'Debug', 'Release'}
platforms {'x32', 'x64'}
configuration "Debug"
targetdir "debug/bin"
objdir "debug/obj"
if os.get() == "windows" then
defines '_CRT_SECURE_NO_WARNINGS'
end
configuration "Release"
targetdir "release/bin"
objdir "release/obj"
if os.get() == "windows" then
defines '_CRT_SECURE_NO_WARNINGS'
end
-- the lua library
project 'lualibd'
targetname 'lualibd' -- rename the target library to lua
kind 'SharedLib'
language 'C'
if os.get() == "windows" then
defines {'LUA_BUILD_AS_DLL'}
end
files {'./*.c'}
excludes { "./lua.c", "./luac.c" ,'./myclua.c','./c_lib1.c','./c_lib2.c'}
-- the lua interpret
project 'lua'
kind 'ConsoleApp'
language 'C'
links 'lualibd'
if os.get() == "linux" then
links {'m'}
else
defines {'_CRT_SECURE_NO_WARNINGS'}
end
files {"./lua.c"}
project 'c_lib1'
targetname 'c_lib1'
kind 'ConsoleApp'
language 'C'
if os.get() == "windows" then
defines {'_Win32'}
end
links {"lualibd"}
includedirs {"./"}
files {'./c_lib1.c'}
-- the lua library
project 'mytestlib'
targetname 'mytestlib'
kind 'SharedLib'
language 'C'
if os.get() == "windows" then
defines {'_Win32'}
end
links {"lualibd"}
includedirs {"./"}
files {'./c_lib2.c'}
project 'myclua'
targetname 'myclua'
kind 'ConsoleApp'
language 'C'
if os.get() == "windows" then
defines {'_Win32'}
end
links {"lualibd"}
includedirs {"./"}
files {'./myclua.c'}
使用premake4.exe 加上述配置文件来生成Window 下VS编译工程(Linux GCC 也可以)进行编译。
VS命令行执行
1 >premake4 --file=premake4.lua --os=windows vs2012
2 > devenv c_lib_for_lua.sln /build "Release|Win32"
之后在目录release/bin下就生成了可执行文件和库