C call Lua and solve Lua environmental problems

       It has always been developed on the framework, and one of the advantages of skynet is that you can use lua to write logic, so the C call lua has never been tried by yourself. Now I am training ai, and I just have time to do this.

       First supplement the concept:

       1. C needs to manage its own memory, apply and release

       2. Lua automatically manages memory, and variables that are not referenced will be regularly GC

       Then if c calls lua, if the lua variable is released, it will be a tragedy. So you need to use one thing to always refer to lua variables. The interaction between C and lua is to use the virtual stack to interact (personal understanding).

       Below is the code, these C can use Lua's API to check their specific uses.

        Attach the lua 5.3 Chinese reference manual address: https://www.runoob.com/manual/lua53doc/contents.html#contents

  1. The following is the C code

#include <stdio.h>

extern "C" 
{
	#include "lua.h"
	#include "lauxlib.h"	//这里需要真tm的注意,不是luaxlib
	#include "lualib.h"
}

//调用lua中的add函数
int call_lua_add(lua_State *L)
{
	lua_getglobal(L, "add"); 		//把虚拟机中全局变量 add 压入虚拟机L的栈,这里注意,一定要是全局变量
	lua_pushnumber(L, 123);			//第一个参数入栈
	lua_pushnumber(L, 456); 		//第二个参数入栈
	lua_call(L, 2, 1);				//调用栈中的add函数,2个参数,1个返回值
	int sum = (int)lua_tonumber(L, -1);		//获取栈顶元素(上一步的返回值)
	lua_pop(L, 1);		//栈顶元素出战
	return sum;
}

int main()
{
	lua_State *L = luaL_newstate();		//新建lua虚拟机
	luaL_openlibs(L);					//在虚拟机中载入lua所有函数库
	luaL_dofile(L, "Test.lua");			//加载 并 运行指定的文件
	lua_settop(L, 0);					//重新设置栈底,这个过程,是为了确认栈底是空的,以便后面的操作是按照顺序入栈的且从1号栈位开始
	int ret = call_lua_add(L);
	printf("调用lua文件结果为 %d\n", ret);
	lua_close(L);						//一定记得关闭虚拟机  

	return 0;
}

2. The following is the lua code

function add( x, y )
	return x+y
end

print("你终于动手了")

3. Compile the c file, and then find that an error is reported, the reason is that the header file cannot be found, that is, the environment is not specified

4. Find it briefly

4. Then compile, the -I parameter is used to specify the header file directory (i), and then it is found that an error is reported

This error means that these things were not found, because they are all in the lualib library, just look it up and specify it.

5. I directly sudo cp /usr/lib/x86_64-linux-gnu/liblua5.3.so.0. Copy the library to this directory, and then rename it to liblua5.3.so. Then compile, the -l parameter is used to specify the library to be linked with the program, the -l parameter is followed by the library name (l), no error is reported. run. The result is as follows

       Because I have been using lua for a while, I have always felt that it is not difficult to interact between C and lua, but I feel that I have the power to say whether it is difficult or not until I actually operate it. I tried it today, it's really not difficult. Still need to try as much as possible by myself before drawing conclusions. "There is no sword, and there is no sword, not a concept"

 

Guess you like

Origin blog.csdn.net/banfushen007/article/details/107956696