當前位置:首頁 » 編程軟體 » tolua編譯

tolua編譯

發布時間: 2022-07-24 08:27:23

『壹』 tolua 怎麼bind std:vector

我在嘗試導出Ogre的所有類介面到lua中使用,參考CEGUI的方法,使用的是tolua++來導出C++類對象。在使用過程中,遇到了一些問題。 問題1: 表現為tolua++編譯錯誤,錯誤信息是:***curr code for error is $pfile "OgreBase/OgreVector2.h" 這里...

『貳』 LUA編譯好了,但是tolua++編譯不出來出現以下錯誤

設置頭文件包含目錄。makefile裡面用-I ,如果是ide,一般都有可以圖形化選擇的。

『叄』 有沒有類似的tolua++工具導出c#引用c++的介面

我在嘗試導出Ogre的所有類介面到lua中使用,參考CEGUI的方法,使用的是tolua++來導出C++類對象。在使用過程中,遇到了一些問題。

問題1:
表現為tolua++編譯錯誤,錯誤信息是:***curr code for error is $pfile "OgreBase/OgreVector2.h"

這里我編寫了一個OgreBase.pkg文件(給tolua++用來生成導出導入lua的定義文件)
namespace Ogre
{
$pfile "OgreBase/OgreSharedPtr.h"
$pfile "OgreBase/OgreVector2.h"
};

OgreSharedPtr.h文件內容如下:
$#include "OgreSharedPtr.h"
/// The method to use to free memory on destruction
enum SharedPtrFreeMethod
{
/// Use OGRE_DELETE to free the memory
SPFM_DELETE,
/// Use OGRE_DELETE_T to free (only MEMCATEGORY_GENERAL supported)
SPFM_DELETE_T,
/// Use OGRE_FREE to free (only MEMCATEGORY_GENERAL supported)
SPFM_FREE
};

class SharedPtr
{
TOLUA_TEMPLATE_BIND(T, Ogre::DataStream , Ogre::DataStreamList , Ogre::StringVector , Ogre::FileInfoList)

SharedPtr();
~SharedPtr();
T* get() ;
void bind(T* rep, SharedPtrFreeMethod freeMethod = SPFM_DELETE);
bool unique() ;
unsigned int useCount() ;
T* getPointer() ;
bool isNull(void) ;
void setNull(void);
};
OgreVector2.h內容如下:
$#include "OgreVector2.h"

class Vector2
{
public:
Real x, y;

public:
inline Vector2();
inline Vector2( Real fX, Real fY );
inline Vector2( Vector2& rkVector );

inline Real operator [] ( size_t i ) ;

inline Real& operator [] ( size_t i );
inline Real* ptr();
inline Real* ptr() ;
inline Vector2& operator = ( Vector2& rkVector );
inline Vector2& operator = ( Real fScalar);

inline bool operator == ( Vector2& rkVector ) ;

inline bool operator != ( Vector2& rkVector ) ;
inline Vector2 operator + ( Vector2& rkVector ) ;

inline Vector2 operator - ( Vector2& rkVector ) ;
inline Vector2 operator * ( Real fScalar ) ;

inline Vector2 operator * ( Vector2& rhs) ;

inline Vector2 operator / ( Real fScalar ) ;

inline Vector2 operator / ( Vector2& rhs) ;

inline Vector2& operator + () ;

inline Vector2 operator - () ;

inline Real length () ;
inline Real squaredLength () ;
inline Real dotProct( Vector2& vec) ;
inline Real normalise();
inline Vector2 midPoint( Vector2& vec ) ;
inline bool operator < ( Vector2& rhs ) ;
inline bool operator > ( Vector2& rhs ) ;
inline void makeFloor( Vector2& cmp );
inline void makeCeil( Vector2& cmp );
inline Vector2 perpendicular(void) ;
inline Real crossProct( Vector2& rkVector ) ;
inline Vector2 randomDeviant(
Real angle) ;
inline bool isZeroLength(void) ;
inline Vector2 normalisedCopy(void) ;
inline Vector2 reflect( Vector2& normal) ;
// special points
static const Vector2 ZERO;
static const Vector2 UNIT_X;
static const Vector2 UNIT_Y;
static const Vector2 NEGATIVE_UNIT_X;
static const Vector2 NEGATIVE_UNIT_Y;
static const Vector2 UNIT_SCALE;

};

咋一看,沒任何問題,但是編譯卻錯誤。原因在於tolua++對$pfile的處理有些許問題,它是直接把被包含文件粘貼到一個文件中,而OgreSharedPtr.h最後一行是};,結果};和$pfile "OgreBase/OgreVector2.h"連在一行中了,結果出現編譯錯誤。解決辦法很簡單,在每行$pfile之後加一行空行,或者在每個被包含頭文件後保證最後一行是空行即可。

問題2:
tolua++對嵌套類模板的導出bug。編寫如下的pkg文件,為了導出一個有4個模板參數的std::map類到lua中,而4個模板參數中有個模板參數Ogre::STLAllocator帶有自身的模板參數嵌套,代碼如下:

namespace Ogre
{
class STLAllocator
{
TOLUA_TEMPLATE_BIND(T, int , short, double, VECTOR3, std::string, IModelViewWrapper*, IDisplaySetting::Property, IDisplaySetting::DisplaySize, IDisplaySetting::AntiAliasingLevel, DataStreamPtr, Plane, PlaneBoundedVolume, Polygon*, std::pair<std::string Ogre::BaseResourceGroupManager::ArchiveIndexEntry>, std::pair<std::string Ogre::BaseResourceGroupManager::BaseResourceGroup*>, Ogre::BaseResourceGroupManager::ResourceLocation*)
};

};
namespace std
{
class less
{
TOLUA_TEMPLATE_BIND(T,std::string)
};

class pair
{
TOLUA_TEMPLATE_BIND(F S,std::string Ogre::BaseResourceGroupManager::ArchiveIndexEntry)
pair();
~pair();
F first;
S second;
};

class map
{
TOLUA_TEMPLATE_BIND(K V S A,std::string Ogre::BaseResourceGroupManager::ArchiveIndexEntry std::less<std::string> Ogre::STLAllocator<std::pair<std::string,Ogre::BaseResourceGroupManager::ArchiveIndexEntry> >)

class iterator
{
pair<K,V> operator->();
};

void clear();
bool empty();
int size() ;

std::map<K,V,S,A>::iterator find( K &key);
std::map<K,V,S,A>::iterator begin();
std::map<K,V,S,A>::iterator end();
std::map<K,V,S,A>::reverse_iterator rbegin();
std::map<K,V,S,A>::reverse_iterator rend();

std::pair<std::map<K,V,S,A>::iterator,bool> insert( std::pair<K,V> &value);
std::map<K,V,S,A>::iterator erase(std::map<K,V,S,A>::iterator iter);

map();
~map();
};

};

用tolua++編譯這個pkg文件,生成cpp文件時不會出錯,但編譯cpp文件時會出錯,因為它生成的cpp文件中,模板嵌套的代碼有問題,如下:

生成的錯誤代碼片段1:
std::map<std::string ,Ogre::BaseResourceGroupManager::ArchiveIndexEntry ,std::less<std::string> ,Ogre::STLAllocator<std::pair<std::string,Ogre::BaseResourceGroupManager::ArchiveIndexEntry> >>

生成的錯誤代碼片段2:
std::map<std::string ,Ogre::BaseResourceGroupManager::ArchiveIndexEntry ,std::less<std::string> ,Ogre::STLAllocator<std::pair<std::string,Ogre::BaseResourceGroupManager::ArchiveIndexEntry> >,>
這兩段錯誤代碼中,第一個代碼片段最後兩個尖括弧連接在一起了,錯誤在於最後兩個尖括弧間沒有空格。
第二個代碼片段最後兩個尖括弧間多了個「,」符號。
檢查發現,是tolua++代碼本身有bug,修復如下:
打開tolua++目錄的lua目錄,打開class.lua文件,181行append = string.gsub(append, ">>", "> >"),這里目的是把兩個尖括弧間加空格,但它沒考慮到把這個類型與另一個模板類型嵌套使用的情況,結果出現代碼片段1的bug,解決辦法是改成append = string.gsub(append, ">>", "> > "),即在最後一個尖括弧後自動加一個空格,以解決此問題,187行也有同樣的問題,改成bI = string.gsub(bI, ">>", "> > ")即可。
對於錯誤的代碼片段2,需要打開declaration.lua,定位到148行,改成local template_part = "<"..concat(m, 1, m.n , ",")..">",這樣就能解決代碼片段2的問題,此問題的起因在於此地方用空格來做模板參數的分隔符,但模板尖括弧間又有空格,結果把這個空格自動替換成逗號,導致編譯錯誤。

『肆』 lua 怎麼被c++調用的,或者怎麼調用c++的

添加lua的頭文件:
extern "C"
{
#include "./src/lua.h"
#include "./src/lualib.h"
#include "./src/lauxlib.h"}lua_State *lua = NULL; // 定義一個全局的lua_State指針 // 載入lua庫lua = ::lua_open();
if (lua)
{
::luaopen_base(lua);
::luaopen_debug(lua);
::luaopen_math(lua);
::luaopen_string(lua);
::luaopen_table(lua);
} // 從lua腳本文件中獲取函數並執行 luaL_dofile(lua, "test.lua"); // 解析lua文件
lua_getglobal(lua, "func"); // 獲取func函數
lua_pushnumber(lua, 1); // 對第一個參數壓棧
lua_pushnumber(lua, 2); // 對第二個參數壓棧
if (lua_pcall(lua, 2, 1, 0) != 0) // 執行函數
{
printf("call lua failed!!!");
return 0;
} int nRes = (int)lua_tonumber(lua, -1); // 從棧頂取結果
printf("%d\n", nRes); ::lua_pop(lua, 1); // 將結果彈棧 lua_close (lua); // 關閉lua庫

『伍』 libtolua.a是自己編譯的么

可以找被人編譯的借鑒一下

『陸』 unity slua 和 tolua slua哪個更好

更新LUa其實也是更新資源。 Lua被看作一個資源么。Lua代碼都是運行時才編譯的,不運行的時候就如同一張圖片、一段音頻一樣,都是文件資源;所以更新邏輯只需要更新腳本,不需要再編譯,因而Lua能輕松實現「熱更新」。運行效率由於使用反射,所以成為它與生俱來的詬病。目前有對他的改進,像C#ToLua、Slua。但是Ulua是很成熟的,這個對於項目開發我認為暫時比性能重要!很多人在用,很多坑都已經被人采完。個人比較看好Slua的發展。當然了還有李總的L#走的就不是Lua的路。
網上還有說nLua的,我是新手不了解。 看看權威的解釋:
ulua包含兩種c模式(luajit版+原生luavm版),加之tolua c#提供了直接訪問渠道。所以追求效率的,請選用ulua。但是ulua因為底層使用luajit,而luajit目前不能在WP使用,所以如果ulua支持WP需要第二種原生luavm的底層庫。
nlua包含2種模式(KeraLua c版本)(KopiLuac#版本),它支持全,因為c版本底層用的原始的luavm(非luajit)。但是缺少tolua c#的支持,因此效率略低於ulua,但是支持WP(其他也支持)。

『柒』 如何給lua添加動態鏈接機制,以實現lua調用c++模塊如何編譯

定義如下宏為 1:
Mac: LUA_USE_MACOSX
Linux: LUA_USE_LINUX
Windows: LUA_USE_WIN

『捌』 cocos2dx-lua中怎麼使用自定義類以及tolua++的使用

使用cocos2dx-lua開發,免不了自己定義類,但是如何使用自定義的類的?
先了解下lua如何調用c++的:
lua腳本代碼->通過coocs2dx中間解析層代碼->將其轉換並調用cocos2dx c++的前端代碼
coocs2dx中間解析層代碼都在libs/lua/cocos2dx_support/LuaCocos2d.cpp 這個文件中,想了解的可以自己去看下這個文件。
也就是說,你自己定義了一個類,lua能夠調用你自己定義的類,你的自定義類就必須在LuaCocos2d.cpp這個中間解析文件中申明。
看了LuaCocos2d.cpp這個文件,可能有的同學都暈了,不知道怎麼在LuaCocos2d.cpp中申明自己的定義的類。不過,不用擔心,cocos2dx已經提供了tolua++這個工具自動編譯生成新的LuaCocos2d.cpp文件。
下面開始進入正題。
一、創建一個coocs2dx-lua 的Demo工程,然後在class中自己定義個類。
SNSprite.h
[/cpp]
//
// SNSprite.h
// LuaDemo
//
// Created by LiuYanghui on 13-4-8.
//
//
#ifndef __LuaDemo__SNSprite__
#define __LuaDemo__SNSprite__
#include 「cocos2d.h」
USING_NS_CC;
class SNSprite : public CCSprite{
public:
static SNSprite* create(const char* name);
private:
void initData();
};
#endif /* defined(__LuaDemo__SNSprite__) */
[/cpp]
SNSprite.cpp

C++

local function createSunnyLayer()
local layerSunny = CCLayer:create()

local labTips = CCLabelTTF:create("這個icon圖標就是使用的自定義類", "Arial", 18)
labTips:setPosition(ccp(240,280))
layerSunny:addChild(labTips)

local sp = SNSprite:create("Icon.png")
sp:setPosition(ccp(100,100))
layerSunny:addChild(sp)

return layerSunny
end
-- play background music, preload effec t

添加到scene中:

C++

-- run
local sceneGame = CCScene:create() -- 創建場景
--sceneGame:addChild(createLayerFarm()) -- 將農場層加入場景
--sceneGame:addChild(createLayerMenu()) -- 將菜單界面層加入場景
sceneGame:addChild(createSunnyLayer())
CCDirector:sharedDirector():runWithScene(sceneGame)

OK,xcode編譯運行,就看到效果了。

熱點內容
我的世界伺服器卡領地 發布:2025-02-06 08:50:45 瀏覽:255
我的世界公網ip伺服器 發布:2025-02-06 08:46:28 瀏覽:772
php數組值求和 發布:2025-02-06 08:30:56 瀏覽:819
java類可以作為 發布:2025-02-06 08:28:54 瀏覽:412
sql更改列 發布:2025-02-06 08:22:37 瀏覽:396
創建索引sql 發布:2025-02-06 08:22:29 瀏覽:235
西門子有密碼如何初始化 發布:2025-02-06 08:22:28 瀏覽:594
EV壓縮 發布:2025-02-06 08:21:13 瀏覽:336
配置氯化錫時為什麼要加錫粒 發布:2025-02-06 08:19:33 瀏覽:64
阿里雲伺服器存放在哪裡 發布:2025-02-06 08:11:15 瀏覽:156