2013-01-03 55 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; 
相關問題