2013-01-03 20 views
1

我有一个C++应用程序,我想为这个应用程序设计和提供Lua API,有一些工具可以帮助我吗?也许有一种方法可以标记某种方法并将它们暴露给Lua API层?对于其他语言,我已经看到了可以在解析代码后生成API的工具,Lua有类似的东西吗?来自C++应用程序的Lua api自动构建

+1

请查看http://stackoverflow.com/questions/13615975并更正您的问题。 – prapin

+1

请参阅http://stackoverflow.com/questions/103347/how-do-you-glue-lua-to-c-code。 – mkluwe

回答

4

我是真的感谢LuaBridge其中包括在短短1(ONE!)头文件的非常轻量级的方法在应用程序中

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(); 
} 
包括
+0

不错!它是否有一个工具可以从c/C++声明中生成绑定代码? – kerim

+0

不,但一旦你掌握了它们,它们很容易生成 我编辑了我的答案,包括一个例子 – Alar

+0

是的,很简单。我现在明白了 - 这很容易,你不需要自动生成代码? – kerim

1

退房SWIG。根据您的需求,以及如何“清除”你的C/C++头,你可以只给整个.h文件痛饮或选择功能/要导出到Lua类(比如在this基本的例子):

%module example 
%{ 
#include "example.h" 
%} 
int gcd(int x, int y); 
extern double Foo; 
相关问题