Code Monkey home page Code Monkey logo

lua-in-cpp's Introduction

Lua in C++

A simple bridge between Lua's API and C++.

This repository is still work in progress but starts to be useable. You can find classes to use Lua's API in C++ and use it to script things into your C++ programs.

This has been tested with C++ 20 on g++, Lua 5.4 and Ubuntu 22.04.

Installation

Clone this repository and compile the static or shared library (see this) like this:

make libluaincpp.a
make libluaincpp.so

Then you're free to use and link it with your program.

How to use

Here is a a Lua file and a C++ file showing you some of the features that this library does, you can find these files in the example folder:

function greets(name)
    print("[Lua] Hello "..name)
end

function my_sin_int(x)
    print("[Lua] "..math.sin(x))
end

function is_true(b)
    print("[Lua] ", b == true)
end

function my_sin_float(x)
    print("[Lua] "..math.sin(x))
end

function both_sin(x1, x2)
    my_sin_float(x1)
    my_sin_int(x2)
end

function all(name, b, f, i)
    greets(name)
    is_true(b)
    my_sin_float(f)
    my_sin_int(i)
    return 1234
end

function add_test()
    print("[Lua] "..mymath.add(1, 2))
end
#include <string.h>
#include "LuaInCpp.hpp"

// To call a C++ function in Lua, you have to use the Lua API
// Here's an example:
int add(lua_State *l)
{
    int n = lua_gettop(l); // get the number of arguments

    if (n != 2) {
        lua_pushliteral(l, "bad number of arguments");
        lua_error(l);
    }

    int a = lua_tointeger(l, 1); // get first argument
    int b = lua_tointeger(l, 2); // get second arument

    std::cout << "[C++] " << a << " + " << b << std::endl;
    lua_pushinteger(l, a + b); // the return value
    return 1; // number of argument returned
}

int main()
{
    Lua l;
    l.openLibs();

    l.add("add", &add, "mymath");

    if (l.loadFile("test.lua")) {
        LuaFunction<void(const char *)> greets(l, "greets");
        char *s = strdup("Frodo"); // works with not const too
        greets(s);

        LuaFunction<void(int)> my_sin(l, "my_sin_int");
        long a = 0; // works with long too
        my_sin(a);

        LuaFunction<void(double)> my_sin_float(l, "my_sin_float");
        my_sin_float(2.3);

        LuaFunction<void(bool)> is_true(l, "is_true");
        is_true(false);

        LuaFunction<void(double, int)> both_sin(l, "both_sin");
        both_sin(2.3, 4);

        std::cout << std::endl;

        LuaFunction<int(const char *, bool, double, int)> all(l, "all");
        int b = all(s, false, 2.3, 4);
        std::cout << b << std::endl;

        l.call("main");
    }
}

Compile and run like this:

make test
./test

And the output will be:

[Lua] Hello Frodo
[Lua] 0.0
[Lua] 0.74570521217672
[Lua] 	false
[Lua] 0.74570521217672
[Lua] -0.75680249530793

[Lua] Hello Frodo
[Lua] 	false
[Lua] 0.74570521217672
[Lua] -0.75680249530793
1234

[C++] 1 + 2
[Lua] 3

What is currently being done ?

This library is able to do:

  • Injecting C++ values, functions and tables in a lua instance
  • Call thoses C++ functions in lua
  • Call lua functions in C++ (cannot call functions with multiple return values for now)

Known issues

Why can't I compile it in a shared library ?

To compile this project into a shared library, you must also have a shared library of lua itself. For that, you can go here to download the source code of your favorite version of lua and tweak the Makefile in the src folder to create a liblua.so and place it next to the liblua.a already installed on your system

Then you can have this library in a shared format like that:

make libluaincpp.so

Other issues

If you have any idea on how to solve these problems or you want to contribute, please open an issue, I'll be happy to discuss it with you

  • Can't call lua functions with multiple return values
  • The library isn't installed on the machine for now, I believe it's only a few Makefile changes and it would be okay

Support me

You can star this repository, follow me or buy me a coffee

lua-in-cpp's People

Contributors

volpi-m avatar

Stargazers

Théo HEMMER avatar Florent avatar Solange avatar  avatar Élise Valentin avatar Timur avatar ratest avatar SpiritGY avatar  avatar  avatar  avatar  avatar  avatar Kacper Kostka avatar  avatar SealtielFreak avatar

Watchers

James Cloos avatar  avatar

Forkers

spiritgy

lua-in-cpp's Issues

Ubuntu 18.04 with C++17 and gcc --version gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 fails

I compiled this project with Ubuntu 18.04, had to downgrade to C++17. The makefile failed (src/main.o: In function main': main.cpp:(.text+0x9c): undefined reference to Lua::Lua()'), but this command worked:

g++ -fPIC -shared -W -Wall -Wextra --std=c++17 -I/home/savage2/libdir/lua/lua-5.4.4/src/ -L/home/savage2/libdir/lua/lua-5.4.4/src/ BaseFunction.cpp Lua.cpp LuaFunction.cpp NonVoidFunction.cpp VoidFunction.cpp main.cpp -o libluaincpp.so

When I try test.lua and the main programm from the readme it fails.

g++ main.cpp -I ../src/ -I ../../../lua/lua-5.4.4/src/ -L../src/ -L../../../lua/lua-5.4.4/src/ -llua -lluaincpp -fpermissive
main.cpp: In function 'int main()':
main.cpp:14:32: error: no matching function for call to 'add(const char [4], int (*)(int, int), const char [7])'
     l.add("add", &add, "mymath");
                                ^
In file included from ../src/LuaInCpp.hpp:3:0,
                 from main.cpp:1:
../src/Lua.hpp:27:10: note: candidate: void Lua::add(const string&, lua_CFunction, const string&) <near match>
     void add(const std::string &, lua_CFunction, const std::string & = "");
          ^~~
../src/Lua.hpp:27:10: note:   conversion of argument 2 would be ill-formed:
main.cpp:14:18: warning: invalid conversion from 'int (*)(int, int)' to 'lua_CFunction {aka int (*)(lua_State*)}' [-fpermissive]
     l.add("add", &add, "mymath");
                  ^~~~
In file included from ../src/LuaInCpp.hpp:3:0,
                 from main.cpp:1:
../src/Lua.hpp:29:10: note: candidate: void Lua::add(const string&, lua_Integer, const string&) <near match>
     void add(const std::string &, lua_Integer, const std::string & = "");
          ^~~
../src/Lua.hpp:29:10: note:   conversion of argument 2 would be ill-formed:
main.cpp:14:18: warning: invalid conversion from 'int (*)(int, int)' to 'lua_Integer {aka long long int}' [-fpermissive]
     l.add("add", &add, "mymath");
                  ^~~~
In file included from ../src/LuaInCpp.hpp:3:0,
                 from main.cpp:1:
../src/Lua.hpp:31:10: note: candidate: void Lua::add(const string&, void*, const string&) <near match>
     void add(const std::string &, void *, const std::string & = "");
          ^~~
../src/Lua.hpp:31:10: note:   conversion of argument 2 would be ill-formed:
main.cpp:14:18: warning: invalid conversion from 'int (*)(int, int)' to 'void*' [-fpermissive]
     l.add("add", &add, "mymath");

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.