I want to write a C++-Program that can interact/call Lua-scripts during execution. A key concept of the program is complete platform independence, but I seem to be unable to locate a Lua-build that actually offers something that.
The Lua-builds I found so far are either based on environment-variables or specific libraries like .lib
, .dll
or .so
. The official Lua-source from lua.org also is not what I'm looking for as it defines a main-function…
Is there a simple - best case would be something like sqlite-amalgamation - Lua-interpreter for C/C++ that doesn't have any of these dependencies?
In order to compile embedded Lua in C, we need to first write a Lua program followed by a C program that will invoke the Lua program function, and then we will compile the C program. It should be noted that the above Lua script should be saved as Script.
Lua is a general-purpose embedded programming language designed to support procedural programming with data-description facilities.
lua file and automatically runs it on Notepad++ startup. You can easily edit this file via Plugins > LuaScript > Edit Startup Script . You can include any commands you want to immediately execute on program startup, as well as register any additional shortcuts or callbacks.
lua.c contains main and defines the entry point for a console application. If you remove it from the project, whats left builds into a standalone lib, or dynamic library if you prefer, just fine.
The following is what i use as a starting-point for my projects (I found something similar a while back and adapted it so I can change it faster):
The lua script file:
-- Start
-- Script: myscript.lua
print("this is lua")
-- End
The C file:
#include <stdlib.h>
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main(char argv[], int argc) {
static const luaL_reg lualibs[] = {
{ "base", luaopen_base },
{ NULL, NULL }
};
static void openlualibs(lua_State *l) {
const luaL_Reg *lib;
for (lib = lualibs; lib->func != NULL; lib++) {
lib->func(l);
lua_settop(l, 0);
}
}
lua_State *l;
l = lua_open();
openlualibs(l);
printf("now calling lua\n\n");
lua_dofile(l, "myscript.lua");
printf("\ndo something else\n\n");
lua_close(l);
return 0;
}
You can use this freely as a basis for your projects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With