win c++使用lua环境配置 5.3.5版本

编译lua

下载lua源码,github仓库

使用vs编译源码,新建一个静态库项目(只会生成lib文件),想要dll的话就新建dll项目(有一个lib文件和dll文件)

把lua源码下面的文件夹都是,复制到vs项目中
lib目录是我手动建的,我把编译好的lib文件放lib目录下了
sourcecode
点击生成就好了

在c++中使用lua

包含目录写上面的liblua/include
包含目录

库目录写刚编译出来的lib目录,上图的lib目录

lib库目录

链接器添加lualib.lib
链接器

c++ 测试demo示例

c++要include lua.hpp 不然会报错

// lua.hpp
// Lua header files for C++
// <<extern "C">> not supplied automatically because Lua also compiles as C++

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

这里的extern "C"就是告诉编译器按c格式来。兼容c++

#include <lua.hpp>

#include <iostream>


using namespace std;

static void StackDump(lua_State* L) {
	int top = lua_gettop(L);
	for (int i = 1; i <= top; ++i) {
		int t = lua_type(L, i);
		switch (t) {
		case LUA_TSTRING: {
			cout << lua_tostring(L, i);
			break;
		}
		case LUA_TBOOLEAN: {
			cout << lua_toboolean(L, i);
			break;
		}
		case LUA_TNUMBER: {
			if (lua_isinteger(L, i))
				cout << lua_tointeger(L, i);
			else
				cout << lua_tonumber(L, i);
			break;
		}
		default: {
			cout << lua_typename(L, i);
			break;
		}
		}
		cout << " ";
	}
	cout << endl;
}

int main()
{
	lua_State* L = luaL_newstate();
	lua_pushboolean(L, 1);
	lua_pushnumber(L, 10);
	lua_pushnil(L);
	lua_pushstring(L, "hello");
	StackDump(L);
	return 0;
}

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值