Lua环境搭建

Lua环境搭建

因为Lua是脚本语言,一般都是直接寄生到宿主程序中运行。现在自己搭建一个简易的环境用于自己使用,记录一下搭建过程:

1.先去官方下载一个 http://www.lua.org/download.htmlLua最新的包。

2.然后我们来写CMAKE指令如下:(CMakeLists.txt)

CMAKE_MINIMUM_REQUIRED(VERSION 3.17 FATAL_ERROR)

project(LuaTest)
 
include_directories(AFTER ${CMAKE_SOURCE_DIR})
 
##########lua静态库############################################
set(LIB_FILES lapi.c lcode.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 lundump.c lvm.c lzio.c lauxlib.c lbaselib.c lcorolib.c ldblib.c liolib.c lmathlib.c loadlib.c loslib.c lstrlib.c ltablib.c lutf8lib.c linit.c)
 
source_group("\\libFiles" FILES ${LIB_FILES})
 
add_library (LuaLib SHARED ${LIB_FILES})
 
 
###########c++与lua交互###################
add_executable(LuaWithCPPTest source.cpp)
 
target_link_libraries(LuaWithCPPTest LuaLib)
 
 
############lua解释器###########
add_executable(LuaInterpreter lua.c ${LIB_FILES})
 
############DLL#################
add_library(LuaDll SHARED sourceDll.cpp)
target_link_libraries(LuaDll LuaLib)
 
#ADD_DEFINITIONS(-DLUA_LIB -DLUA_BUILD_AS_DLL)
######################################define LUA_LIB##################################################
######################################define LUA_BUILD_AS_DLL#########################################

将该文件放于这个文件中在这里插入图片描述
还要再加入两个文件到src中,目的为了演示我们搭建的环境。一个是source.cpp代码如下:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

extern "C"
{

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
};

void TestLua();

int main()
{
	TestLua();
	return 0;
}

static int fun(lua_State *L) {
	if(L == nullptr){
		return 0;
	}

	cout << "Use c++ function in lua!" << endl;
	return 0;
}

LUALIB_API int funA(lua_State *L) {
	if (L == nullptr) {
		return 0;
	}
	int n = lua_gettop(L);
	if (n != 2) {
		lua_pushstring(L,"数据错误");
		lua_error(L);
		return 0;
	}

	int a = luaL_checkinteger(L, -2);
	int b = luaL_checkinteger(L, -1);

	cout << "  <以下是c++中的输出 >" << endl;
	cout << " a = " << a << " b =" << b << endl;
	cout << "------" << endl;
	
	lua_pushnumber(L, a+b);
	lua_pushnumber(L, b-a);
	return 2;
}

void TestLua()
{
	lua_State *L = luaL_newstate();
	luaopen_base(L); //
	luaopen_table(L); //
	luaopen_package(L); //
	luaopen_io(L); //
	luaopen_string(L); //

	luaL_openlibs(L); //打开以上所有的lib

	string str;
	
	int temp_value = 100;
	lua_pushnumber(L, temp_value);
	lua_setglobal(L, "_tempvalue");
	int temp_value1 = 10000;
	lua_pushnumber(L, temp_value1);
	lua_setglobal(L, "_tempvalue1");

	lua_register(L, "_fun", fun);


	lua_pushcfunction(L, funA);

	lua_setglobal(L, "_funA");
	while (true)
	{
        cout << "请输入Lua代码:" << endl;
		getline(cin, str, '\n');

		if (luaL_loadfile(L, str.c_str()) || lua_pcall(L, 0, 0, 0)) {
			const char * error = lua_tostring(L, -1);
			cout << string(error) << endl;
		}
	

	}
	
	
	lua_close(L);


}

另一个是sourceDll.cpp,代码如下:

extern "C"
{
 
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
};
 
 
static int Test(lua_State *L)
{
    printf("This is a message from c++ dll\n");
    return 0;
}

static int Add(lua_State *L) {
	int n = lua_gettop(L);
	if (n != 2) {
		lua_pushstring(L, "数据错误");
		lua_error(L);
		return 0;
	}
	int a = luaL_checkinteger(L, -2);
	int b = luaL_checkinteger(L, -1);
	int sum = a + b;
	lua_pushnumber(L, sum);
	return 1;

}


 
static const struct luaL_Reg mydlllib[] = {
    {"_Test", Test},
	{"_Add",Add},
    {NULL,NULL},
};
 
extern "C" int __declspec(dllexport)luaopen_LuaDll(lua_State *L)
{
    printf("luaopen_LuaDll invoked\n");
    luaL_newlib(L, mydlllib); // 5.2之前使用luaL_register(L, "modulename", modulename);
 
    return 1;
}


3.接着我们通过CMAKE工具生成就好,CMAKE工具要自己下载哦。
在这里插入图片描述
4.接着我们到生成的路径中打开解决方案,编译这两个。
在这里插入图片描述
5.接下来,我们就可以写Lua 或者和c交互了。
写Lua ,我推荐一个IDE,Sublime Text ,可以自己去下载。
安装后,我们就可以用上刚才热乎的lua解释器啦
在这里插入图片描述

{
	"cmd": ["E:/Lua_new/Debug/LuaInterpreter.exe", "$file"],   --前面这个就是你刚才工程里面生成lua解释器的路径。
	"file_regex": "^(?:lua:)?[\t ](...*?):([0-9]*):?([0-9]*)",  
	"selector": "source.lua" 
}

然后保存命名自己构建方式就可以使用啦。
在这里插入图片描述
选择自己刚才命名的就可以使用啦。


感谢前人栽的树:https://www.cnblogs.com/cdh49/p/3602211.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值