Exploring Lua
To install Lua in Ubuntu:
1 sudo apt-get install libreadline-dev
2 curl -L -R -O
https://www.lua.org/ftp/lua-5.4.6.tar.gz
3 tar zxf lua-5.4.6.tar.gz
4 cd lua-5.4.6
5 make all test
6 make install
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(lua LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Lua REQUIRED)
add_executable(lua main.cpp)
target_link_libraries(lua ${LUA_LIBRARIES})
install(TARGETS lua LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
Example:
#include <lua.hpp>
#include <iostream>
#include <string>
int main() {
std::string cmd = "a=1+1";
lua_State* L = luaL_newstate();
int r = luaL_dostring(L, cmd.c_str());
if(r == LUA_OK)
{
lua_getglobal(L,"a");
if(lua_isnumber(L, -1))
{
float af =(float) lua_tonumber(L,-1);
std::cout<<"af "<<af<<'\n';
}
}
else
{
std::string errmsg=lua_tostring(L,-1);
std::cout<<errmsg<<'\n';
}
return 0;
}