Reputation: 1734
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?
Upvotes: 8
Views: 11803
Reputation: 23550
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.
Upvotes: 9
Reputation: 43286
For a single-file amalgamation build of the Lua core and stand-alone interpreter, see the file etc/all.c
in the official source kit. You can certainly use it as a basis for your own amalgamation, perhaps by eliminating the reference to lua.c
on the last line.
Many larger applications that embed Lua do it by simply adding the core source files to the project. This is particularly effective if the application intends to not permit extension code to load compiled modules from .dll
or .so
files. If your application will permit compiled modules, then it is usually best to link against the shared library for the core so that the application and loaded modules can reference the symbols from a single instance of the core library. Note that accidental inclusion of multiple instances of the Lua core is almost guaranteed to produce very hard to diagnose symptoms.
Upvotes: 4
Reputation: 36016
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.
Upvotes: 8