新建C++工程
- 选择:
C++ Executable
- 设置工程存放路径:
Location
- 选择C++标准:
C++14
配置CLion中lua配置
Lua interpreter:
C:\Lua\lua.exe
Working directory:
D:\Projects\Work Space\CLion\TestLua\lua
Environment variables:
Lua=C:\Lua
引入lua文件
- 建立
include
文件夹,并将lua源码中*.h
*.hpp
文件复制到此处
配置CMakeLists.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
cmake_minimum_required(VERSION 3.16) project(TestLua) ##C++14标准 set(CMAKE_CXX_STANDARD 14) ##定义变量 set(AET_FILE D:/Projects/Work\ Space/CLion/TestLua) ##设置工程输出目录 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${AET_FILE}/bin) ##包含头文件 include_directories(${AET_FILE}/include) add_executable(TestLua src/main.cpp) |
编写lua代码
- lua
- test.lua
1 2 3 4 5 |
print("test") function event() print("event test") end |
编写C++代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> #pragma comment(lib,"C:/Lua/lua-5.3.5.lib") extern "C" { #include "../include/lua.h" #include "../include/lualib.h" #include "../include/lauxlib.h" } int main() { lua_State * lua = luaL_newstate(); luaL_openlibs(lua); luaopen_base(lua); if(luaL_loadfile(lua,"../lua/test.lua")) { const std::string error = lua_tostring(lua,-1); std::cout << "lua file load error:" << error << std::endl; return -1; } if(lua_pcall(lua,0,0,0)) { const std::string error = lua_tostring(lua,-1); std::cout << "lua pcall error:" << error << std::endl; return -1; } lua_getglobal(lua,"event"); std::cout << lua_gettop(lua) << std::endl; if(lua_pcall(lua,0,0,0) != 0) { std::cout << "call event failed:" << lua_tostring(lua,-1) << std::endl; lua_pop(lua,1); } std::cout << lua_gettop(lua) << std::endl; lua_close(lua); system("pause"); return 0; } |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ C++_成员访问权限06/20
- ♥ 51CTO:Linux C++网络编程三08/16
- ♥ C++并发编程_概念了解05/07
- ♥ C++17_第一篇12/20
- ♥ WindowsETW进程监控相关03/17
- ♥ C++程序高级调试与优化_第一篇07/20