c/c++中调用lua第一个例子

首先下载lua源码包,然后分别是make,make linux,make install

注意如果没有make install,那么在#include<lua.h>时,会报找不到lua.h文件的错误。

网上找到一段源码:

func.lua

--变量定义
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.c

#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,结果报错:

/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 status

网上搜了下,需要加上lua静态库文件:

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

这时编译通过。

时间: 2024-08-08 13:46:14

c/c++中调用lua第一个例子的相关文章

C中调用Lua函数

我们先来看一个简单的例子: lua_State* L = NULL; // 内部调用lua函数 double f(double x, double y) { double z; lua_getglobal(L, "f"); // 获取lua函数f lua_pushnumber(L, x); // 压入参数x和y lua_pushnumber(L, y); if(lua_pcall(L, 2, 1, 0) != 0) error(L, "error running functi

C语言中调用Lua

C语言和Lua天生有两大隔阂: 一.C语言是静态数据类型,Lua是动态数据类型 二.C语言需要程序员管理内存,Lua自动管理内存 为了跨越世俗走到一起,肯定需要解决方案. 解决第一点看上去比较容易,C语言中有union. 可是第二点呢?万一C语言正引用着Lua的一个值,Lua自动释放了怎么办? 所以就有了一种比union更好的解决方案:栈. 这是一个虚拟的栈,是沟通两者的桥梁,两者的数据交换全都通过这个栈进行,这样只要不pop,Lua就不会自动释放,而什么时候pop由C语言说了算. 下面是一段喜

vs如何在C++中调用Lua

最近Cocos2dx的学习卡壳了,一般的照抄代码我不想写上来,但想示例也想得我头晕...为了放松大脑调整状态于是开始学习Lua.Lua的语法学习还是比较简单的,学过javascript或者vbscript的应该很容易就能上手,那些Lua独有的特性也是比较有趣,例如不定数目的多参数函数和随意的参数返回值等. 这里想要吐槽一下最近用来学习Lua的书籍<XX开发实践指南>(虽然没有写全名不过搜索过Lua学习资料的童鞋应该都懂是哪本书),不知道是作者问题还是译者问题,有些地方的解释说明有点糟糕,要么不

vs2013如何在C++中调用Lua(二)

Lua学习笔记 vs2013如何在C++中调用Lua (此为转载教程) 本人试过完全可行 一.准备工作 1.下载Lua源码,地址:http://www.lua.org/download.html(我用的是目前最新版5.2.3) 2.将源码放在合适的盘(我的在D盘,路径D:/Lua-5.2.3/src) 3.打开vs2013新建一个win32控制台应用程序(Win32 console project ),我将他取名为LuaLib 4.确定后,会弹出应用程序向导的提示框,点击下一步.应用程序类型选择

在C\C++中调用Lua

1.C\C++程序中调用Lua函数 方法一: #include "stdafx.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include "lua.hpp" const char* lua_function_code = "function dealStr(str) \   return string.gsub(str,\"Wo

c/c++中调用lua

本人最近学了lua,做点记录.. 要在c/c++中使用lua,当时首先是需要个lua的解析器啦...  从( http://www.lua.org/ ) 这个网站可以下到.本人是在这里下载的 lua5.3 的源码, 可以从 这个git库clone一份  本人的vs2013工程 ( https://git.oschina.net/liLinux/lua-5.3.git )..已经做好了设置,可以编译成静态库或者动态库都行. c中调用lua,很基础很简单.所以直接上代码 //callLua.lua 

cocos2d-x 3.0 在C++中调用lua函数(2)

个人觉得3.0里面, 在C++下面调用lua函数很不方便, 所以就扩展了一个类, 继承自LuaStack, 代码和使用方式如下: #ifndef __CC_LUA_STACKEX_H_ #define __CC_LUA_STACKEX_H_ #include "CCLuaStack.h" NS_CC_BEGIN class LuaStackEx : public LuaStack { public: void call_script_fun(const char* fun) { exe

cocos2d-x 3.0 在C++中调用lua函数

代码用的是<cocos2d-x 3.0 在lua中调用自定义类>中的代码. 在上篇的基础上进行扩充. 写lua函数 local function process_packet(user_data) if user_data then user_data = tolua.cast(user_data, "user_data"); print (user_data:uid()); print (user_data:uname()); end end local ghall =

在C中调用Lua代码

这个程序从终端读入内容,而后按照lua块执行. #include <stdio.h> #include <string.h> #include "lua.h" #include "lauxlib.h" #include "lualib.h" int main(){ char buff[1024]; int error; memset(buff, 0, sizeof(buff)); lua_State *L = luaL_n