分享

C++ Builder编译Lua 5.1静态库以及使用的方法

 quasiceo 2014-01-12

C++ Builder编译Lua 5.1静态库以及使用的方法

(2013-04-08 09:57:28)
标签:

教育

分类: 程序人生
网上有个英文版本的,直接Copy,Mark一下

This shortly manual how work with lua script engine from Borland C++ 6.

第一步下载源代码:
First download sources: http://www./ftp/lua-5.1.tar.gz
Next:
- start BCB6->File->New->Other->Library
- Add to Project->C File (*.c)->Select all .c files from unpacked sources and and click open
- Save All
- Build
- Go to directory with your saved project
- copy new .lib file to $(BCB)/lib
- go to $(BCB)/include and create file lua.hpp with this content:
Code: [Select]
// lua.hpp
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

- copy lua.h,lualib.h,luaxlib.h,luaconf.h to $(BCB)/include
Create test program:
- File->New->Application
- Add to Project->Library file (.lib)->YourCompiledLibFileWithLua.lib
- Drop button to form
- edit header project:
Code: [Select]
//---------------------------------------------------------------------------

#ifndef LuaMainH
#define LuaMainH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TButton *Button1;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private: // User declarations
public: // User declarations
        __fastcall TForm1(TComponent* Owner);
};
int l_ShowMessage( lua_State* L );
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


- edit code project
Code: [Select]
//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "LuaMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
lua_State *L;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
        L = lua_open();
        luaopen_base(L);
        luaopen_table(L);
        luaopen_string(L);
        luaopen_math(L);
        luaopen_debug(L);

        lua_register(L,"ShowMessage",l_ShowMessage);
}
//---------------------------------------------------------------------------
int l_ShowMessage( lua_State* L )
{
ShowMessage(AnsiString( lua_tostring(L, -1) ));
lua_pushnumber( L, 0 );
return 1;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int ret = luaL_loadfile(L,"Hello.lua");
        if ( ret == 0 ) {
        lua_pcall(L, 0, LUA_MULTRET, 0);//execute Hello.lua
        }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
        lua_close(L);        
}
//---------------------------------------------------------------------------

- Save All
- create file Hello.lua inside project directory with this content
Code: [Select]
ShowMessage("I say hello to you");
- Build project
- Run Project
- Push the button.

That\'s all

C++ Builder编译Lua 5.1静态库以及使用的方法

(2013-04-08 09:57:28)
标签:

教育

分类: 程序人生
网上有个英文版本的,直接Copy,Mark一下

This shortly manual how work with lua script engine from Borland C++ 6.

第一步下载源代码:
First download sources: http://www./ftp/lua-5.1.tar.gz
Next:
- start BCB6->File->New->Other->Library
- Add to Project->C File (*.c)->Select all .c files from unpacked sources and and click open
- Save All
- Build
- Go to directory with your saved project
- copy new .lib file to $(BCB)/lib
- go to $(BCB)/include and create file lua.hpp with this content:
Code: [Select]
// lua.hpp
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

- copy lua.h,lualib.h,luaxlib.h,luaconf.h to $(BCB)/include
Create test program:
- File->New->Application
- Add to Project->Library file (.lib)->YourCompiledLibFileWithLua.lib
- Drop button to form
- edit header project:
Code: [Select]
//---------------------------------------------------------------------------

#ifndef LuaMainH
#define LuaMainH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TButton *Button1;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private: // User declarations
public: // User declarations
        __fastcall TForm1(TComponent* Owner);
};
int l_ShowMessage( lua_State* L );
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


- edit code project
Code: [Select]
//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "LuaMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
lua_State *L;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
        L = lua_open();
        luaopen_base(L);
        luaopen_table(L);
        luaopen_string(L);
        luaopen_math(L);
        luaopen_debug(L);

        lua_register(L,"ShowMessage",l_ShowMessage);
}
//---------------------------------------------------------------------------
int l_ShowMessage( lua_State* L )
{
ShowMessage(AnsiString( lua_tostring(L, -1) ));
lua_pushnumber( L, 0 );
return 1;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int ret = luaL_loadfile(L,"Hello.lua");
        if ( ret == 0 ) {
        lua_pcall(L, 0, LUA_MULTRET, 0);//execute Hello.lua
        }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
        lua_close(L);        
}
//---------------------------------------------------------------------------

- Save All
- create file Hello.lua inside project directory with this content
Code: [Select]
ShowMessage("I say hello to you");
- Build project
- Run Project
- Push the button.

That\'s all

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多