首先下載lua源碼包,而後分別是make,make linux,make installlinux
注意若是沒有make install,那麼在#include<lua.h>時,會報找不到lua.h文件的錯誤。函數
網上找到一段源碼:lua
func.luaspa
--變量定義
width=1 ;
height=2 ;
--lua函數定義,實現加法
function sum(a,b)
return a+b ;
end
--lua函數定義,實現字符串相加
function mystrcat(a,b)
return a..b ;
end
--lua函數定義,經過調用c代碼中的csum函數實現加法
function mysum(a,b)
return csum(a,b) ;
end字符串
test_lua.cget
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
//lua頭文件
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#define err_exit(num,fmt,args) \
do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);exit(num);} while(0)
#define err_return(num,fmt,args) \
do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);return(num);} while(0)
//lua中調用的c函數定義,實現加法
int csum(lua_State* l)
{
int a = lua_tointeger(l,1) ;
int b = lua_tointeger(l,2) ;
lua_pushinteger(l,a+b) ;
return 1 ;
}
int main(int argc,char** argv)
{
lua_State * l = luaL_newstate() ; //建立lua運行環境
if ( l == NULL ) err_return(-1,"luaL_newstat() failed");
int ret = 0 ;
ret = luaL_loadfile(l,"func.lua") ; //加載lua腳本文件
if ( ret != 0 ) err_return(-1,"luaL_loadfile failed") ;
ret = lua_pcall(l,0,0,0) ;
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
lua_getglobal(l,"width"); //獲取lua中定義的變量
lua_getglobal(l,"height");
printf("height:%ld width:%ld\n",lua_tointeger(l,-1),lua_tointeger(l,-2)) ;
lua_pop(l,1) ; //恢復lua的棧
int a = 11 ;
int b = 12 ;
lua_getglobal(l,"sum"); //調用lua中的函數sum
lua_pushinteger(l,a) ;
lua_pushinteger(l,b) ;
ret = lua_pcall(l,2,1,0) ;
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
printf("sum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
lua_pop(l,1) ;
const char str1[] = "hello" ;
const char str2[] = "world" ;
lua_getglobal(l,"mystrcat"); //調用lua中的函數mystrcat
lua_pushstring(l,str1) ;
lua_pushstring(l,str2) ;
ret = lua_pcall(l,2,1,0) ;
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
printf("mystrcat:%s%s = %s\n",str1,str2,lua_tostring(l,-1)) ;
lua_pop(l,1) ;
lua_pushcfunction(l,csum) ; //註冊在lua中使用的c函數
lua_setglobal(l,"csum") ; //綁定到lua中的名字csum
lua_getglobal(l,"mysum"); //調用lua中的mysum函數,該函數調用本程序中定義的csum函數實現加法
lua_pushinteger(l,a) ;
lua_pushinteger(l,b) ;
ret = lua_pcall(l,2,1,0) ;
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
printf("mysum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
lua_pop(l,1) ;
lua_close(l) ; //釋放lua運行環境
return 0 ;
}
源碼
直接編譯,cc test_lua.c,結果報錯:string
/tmp/ccuUFpHd.o: In function `csum':
test_lua.c:(.text+0x1b): undefined reference to `lua_tointegerx'
test_lua.c:(.text+0x31): undefined reference to `lua_tointegerx'
test_lua.c:(.text+0x46): undefined reference to `lua_pushinteger'
/tmp/ccuUFpHd.o: In function `main':
test_lua.c:(.text+0x62): undefined reference to `luaL_newstate'
test_lua.c:(.text+0xb1): undefined reference to `luaL_loadfilex'
test_lua.c:(.text+0x108): undefined reference to `lua_pcallk'
test_lua.c:(.text+0x124): undefined reference to `lua_tolstring'
test_lua.c:(.text+0x15a): undefined reference to `lua_getglobal'
test_lua.c:(.text+0x168): undefined reference to `lua_getglobal'
test_lua.c:(.text+0x17b): undefined reference to `lua_tointegerx'
test_lua.c:(.text+0x191): undefined reference to `lua_tointegerx'
test_lua.c:(.text+0x1b4): undefined reference to `lua_settop'
test_lua.c:(.text+0x1d0): undefined reference to `lua_getglobal'
test_lua.c:(.text+0x1df): undefined reference to `lua_pushinteger'
test_lua.c:(.text+0x1ee): undefined reference to `lua_pushinteger'
test_lua.c:(.text+0x212): undefined reference to `lua_pcallk'
test_lua.c:(.text+0x22e): undefined reference to `lua_tolstring'
test_lua.c:(.text+0x269): undefined reference to `lua_tointegerx'
test_lua.c:(.text+0x28f): undefined reference to `lua_settop'
test_lua.c:(.text+0x2c5): undefined reference to `lua_getglobal'
test_lua.c:(.text+0x2d2): undefined reference to `lua_pushstring'
test_lua.c:(.text+0x2df): undefined reference to `lua_pushstring'
test_lua.c:(.text+0x303): undefined reference to `lua_pcallk'
test_lua.c:(.text+0x31f): undefined reference to `lua_tolstring'
test_lua.c:(.text+0x35a): undefined reference to `lua_tolstring'
test_lua.c:(.text+0x382): undefined reference to `lua_settop'
test_lua.c:(.text+0x395): undefined reference to `lua_pushcclosure'
test_lua.c:(.text+0x3a3): undefined reference to `lua_setglobal'
test_lua.c:(.text+0x3b1): undefined reference to `lua_getglobal'
test_lua.c:(.text+0x3c0): undefined reference to `lua_pushinteger'
test_lua.c:(.text+0x3cf): undefined reference to `lua_pushinteger'
test_lua.c:(.text+0x3f3): undefined reference to `lua_pcallk'
test_lua.c:(.text+0x40f): undefined reference to `lua_tolstring'
test_lua.c:(.text+0x447): undefined reference to `lua_tointegerx'
test_lua.c:(.text+0x46d): undefined reference to `lua_settop'
test_lua.c:(.text+0x476): undefined reference to `lua_close'
collect2: ld returned 1 exit statusit
網上搜了下,須要加上lua靜態庫文件:io
cc test_lua.c -llua,
這時以前報的錯誤都沒了,又報新錯誤
/usr/local/lib/liblua.a(lobject.o): In function `luaO_arith':
lobject.c:(.text+0x5bf): undefined reference to `floor'
lobject.c:(.text+0x5f2): undefined reference to `pow'
/usr/local/lib/liblua.a(lvm.o): In function `luaV_execute':
lvm.c:(.text+0x23b9): undefined reference to `floor'
lvm.c:(.text+0x23f6): undefined reference to `pow'
collect2: ld returned 1 exit status
而後把命令改爲:
cc test_lua.c -llua -lm
這時編譯經過。