Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed Lua into C++

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?

like image 921
MFH Avatar asked Feb 26 '11 13:02

MFH


People also ask

Can Lua be compiled to C?

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.

Is Lua embedded?

Lua is a general-purpose embedded programming language designed to support procedural programming with data-description facilities.

Can Notepad ++ run Lua?

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.


2 Answers

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.

like image 125
Chris Becke Avatar answered Sep 28 '22 16:09

Chris Becke


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.

like image 38
Bernd Elkemann Avatar answered Sep 28 '22 15:09

Bernd Elkemann