1

I have a C++ application and I would like to design and offer Lua APIs for this application, there is some tool that can help me with that? Maybe there is a way to mark some method and expose them to the Lua API layer? For other languages I have seen tool that can generate APIs after parsing the code, there is something similar to this for Lua?

Doug Currie
  • 40,708
  • 1
  • 95
  • 119
user1849534
  • 2,329
  • 4
  • 18
  • 20

2 Answers2

4

I truely appreciated the very lightweight approach of LuaBridge which consists in just 1 (ONE!) header file to include in your application

https://github.com/vinniefalco/LuaBridge

https://github.com/vinniefalco/LuaBridgeDemo

/** Declare LUA binding for this class
 *
 * @param global_lua
 */
void c_entity::lua_bind(lua_State* L) {
    getGlobalNamespace(L)
        .beginClass<c_entity>("c_entity")
            .addFunction("getSpeed",&c_entity::get_linear_speed)
            .addFunction("getName",&c_entity::get_name)
            .addFunction("getMaxSpeed",&c_entity::get_max_linear_speed)
            .addFunction("getAcceleration",&c_entity::get_max_linear_acceleration)
            .addFunction("getHull",&c_entity::get_hull)
            .addFunction("getArmor",&c_entity::get_armor)
            .addFunction("getShield",&c_entity::get_shield)
            .addCFunction("getStatus",&c_entity::getStatus)
            .addFunction("logTrace",&c_entity::log_trace)
            .addFunction("logInfo",&c_entity::log_info)
            .addFunction("logDebug",&c_entity::log_debug)
            .addFunction("logError",&c_entity::log_error)
        .endClass();
}
Alar
  • 760
  • 4
  • 11
  • Nice! Does it have a tool to generate bindings code from c/c++ declarations? – kerim Jan 06 '13 at 14:16
  • No, but once you get a grasp on it they are very easy to generate I edited my answer to include an example – Alar Jan 10 '13 at 09:32
  • yea, quite simple. I understand it now - it's so easy that you don't need automatic code generation? – kerim Jan 10 '13 at 09:58
  • Well le't say it's so easy you can write your own generation tool, if you want :D I just did it by hand because I need to expose only a very limited subset of class method – Alar Jan 10 '13 at 11:16
1

Check out SWIG. Depending on your needs and how "clear" your C/C++ headers you can just feed entire .h files to SWIG or select functions/classes you want to export to Lua(like in this basic example):

%module example
%{
#include "example.h"
%}
int gcd(int x, int y);
extern double Foo;
kerim
  • 2,412
  • 18
  • 16