Run Lua code block inside C++

Asked

Viewed 463 times

2

How do I run a Lua code block inside a C function++?

The idea would be something like:

int main() {
  tipodavariavel script;
  script << "print('Ola mundo')";
  executar(script);
  return 0;
}

1 answer

2


This is an example of basic code to run a script Lua. Note that essentially C syntax is used:

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
 
int main() {
    lua_State *L = lua_open();
    luaL_openlibs(L);
    luaL_dostring(L, "print('Ola mundo')");
    lua_close(L);
}

I put in the Github for future reference.

To compile you must use something like this using GCC/Mingw:

g++ -o interpetadorlua.cpp -llua -ldl

Generally speaking this is, I don’t know if you need anything specific.

List of multiple libraries with examples that can help in integration in many ways if you want higher-level and abstract ways to address the problem.

  • I need to run inline.

  • What do you call inline?

  • I edited the question. Would run straight from there without using another file.

  • luaL_loadstring does not execute code. Use luaL_dostring.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.