Embed Lua into C++

怎甘沉沦 提交于 2019-12-22 05:19:06

问题


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?


回答1:


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.




回答2:


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(void) {
            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.




回答3:


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.



来源:https://stackoverflow.com/questions/5127226/embed-lua-into-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!