• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴(yán)以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            C++程序中嵌入Ruby腳本系統(tǒng)

            作者: 楊粼波
            Email: 
            tx7do#yahoo.com.cn

                突發(fā)奇想的想要把Ruby嵌入到C++的程序里面去,想了于是就去做了.現(xiàn)在只在Windows下面測(cè)試通過,其他系統(tǒng)下,我還沒有試過,不過基本過程大差不差的.

            需要準(zhǔn)備些什么?
                1.Ruby(http://www.ruby-lang.org/en/downloads/),去頁面中下載最新版本的Ruby 的安裝包;
                2.Swig(http://www.swig.org/download.html),去頁面中下載最新的安裝包.

            什么是Ruby?
                Ruby,一種為簡(jiǎn)單快捷面向?qū)ο缶幊蹋嫦驅(qū)ο蟪绦蛟O(shè)計(jì))而創(chuàng)的腳本語言,由日本人松本行弘(まつもとゆきひろ,英譯:Yukihiro Matsumoto,外號(hào)matz)開發(fā),遵守GPL協(xié)議和Ruby License。Ruby的作者認(rèn)為Ruby > (Smalltalk + Perl) / 2,表示Ruby是一個(gè)語法像Smalltalk一樣完全面向?qū)ο?、腳本執(zhí)行、又有Perl強(qiáng)大的文字處理功能的編程語言。

            什么是SWIG?
                SWIG(Simplified Wrapper and Interface Generator)是個(gè)幫助使用C或者C++編寫的軟件能與其它各種高級(jí)編程語言進(jìn)行嵌入聯(lián)接的開發(fā)工具。SWIG能應(yīng)用于各種不同類型的語言包括常用腳本編譯語言例如Perl, PHP, Python, Tcl, Ruby and PHP。
              簡(jiǎn)單來說,主要用于導(dǎo)出C/C++程序庫給腳本語言使用的一個(gè)自動(dòng)化工具.導(dǎo)出的工作是非常機(jī)械化,而且繁復(fù)的.

            編譯環(huán)境設(shè)置
                Ruby在Windows下:
                頭文件在$RUBY_HOME/lib/ruby/1.8/i386-mswin32;
                lib在$RUBY_HOME/lib,為msvcrt-ruby18.lib;
                dll在RUBY_HOME/bin,其實(shí)只有一個(gè)dll,就是:msvcrt-ruby18.dll.
                在這里需要注意到的是,$RUBY_HOME/lib/ruby/1.8/i386-mswin32/config.h這個(gè)文件對(duì)VC的版本做了限制:

            #if _MSC_VER != 1200
            #error MSC version unmatch
            #endif
                所以,如果VC不是這個(gè)版本的話,編譯是通不過的,對(duì)此問題,最簡(jiǎn)單的辦法就是:將這三行代碼注釋掉,就可以了.

            C++解釋器包裹代碼
            頭文件

            #ifndef __RubyInterpreter_H__
            #define __RubyInterpreter_H__


            #include 
            <string>

            typedef unsigned 
            long    VALUE;
            typedef std::
            string        String;

            typedef VALUE(
            *staticValueMethod)();
            typedef VALUE(
            *ProtectedMethod)(VALUE);

            class RubyInterpreter
            {
            public:
                RubyInterpreter();
                
            virtual ~RubyInterpreter();

            public:
                
            /// 初始化解釋器
                void initializeInterpreter();

                
            /// 終止解釋器
                void finalizeInterpreter();

                
            /// 設(shè)置
                void setOutputFunction(staticValueMethod func);

                
            /// 加入引用庫的搜索路徑
                void addSearchPath(const String& path);

            public:
                
            /// 執(zhí)行語句
                bool execute(const String& command);
                
                
            /// 執(zhí)行文件
                bool executeFile(String rubyfile);

            private:
                
            /// 記錄錯(cuò)誤日志
                void logRubyErrors(const std::string& intro, int errorcode);
                
                
            /// 
                void loadProtected(ProtectedMethod func, VALUE args,
                    
            const std::string& msg, bool exitOnFail = false);

                
            /// 
                static VALUE loadDlls(VALUE);
            }
            ;


            #endif
            源文件

            #include 
            "StdAfx.h"
            #include 
            "RubyInterpreter.h"

            #include 
            "FixRubyHeaders.h"
            #include 
            <ruby.h>
            #include 
            "FixRubyHeaders.h"


            RubyInterpreter::RubyInterpreter()
            {

            }


            RubyInterpreter::
            ~RubyInterpreter()
            {

            }


            void RubyInterpreter::initializeInterpreter()
            {
            #if defined(NT)
                
            static int dummyargc(0);
                
            static char** vec;
                NtInitialize(
            &dummyargc, &vec);
            #endif

                
            // 初始化Ruby
                ruby_init();

                
            // 使用UTF8編碼
                execute( "$KCODE = 'u'" );

                
            // addSearchPath();

                
            // 初始化腳本加載路徑
                ruby_init_loadpath();

                
            // 設(shè)置安全級(jí)別
                rb_set_safe_level(0);

                
            // 
                ruby_script("ruby");

                
            //loadProtected(&RubyInterpreter::loadDlls, 0, "Ruby error while loading dlls");
            }


            void RubyInterpreter::finalizeInterpreter()
            {
                ruby_finalize();
            }


            void RubyInterpreter::setOutputFunction(staticValueMethod func)
            {
                rb_defout 
            = rb_str_new(""0);

                
            // 定義一個(gè)虛擬類的方法
                rb_define_singleton_method(rb_defout, "write", func, 1);
            }


            void RubyInterpreter::addSearchPath(const String& path)
            {
                ruby_incpush(path.c_str());
            }


            VALUE RubyInterpreter::loadDlls(VALUE val)
            {
                String lib;

                
            // 
                return rb_require(lib.c_str());
            }


            void RubyInterpreter::loadProtected(ProtectedMethod func,
                                                VALUE val, 
                                                
            const std::string& msg, 
                                                
            bool exitOnFail)
            {
                
            int error = 0;
                rb_protect(func, val, 
            &error);
                logRubyErrors(
            "Ruby error while initializing", error);
            }


            void RubyInterpreter::logRubyErrors(const std::string& intro, int errorcode)
            {
                
            if (errorcode != 0)
                
            {
                    VALUE info 
            = rb_inspect(ruby_errinfo);
                    rb_backtrace();
                    
            if (intro.length() > 0)
                    
            {
                    }

                }

            }


            bool RubyInterpreter::execute(const String& command)
            {
                
            int status = -1;

                rb_eval_string_protect(command.c_str(), 
            &status);

                logRubyErrors(
            "", status);

                
            if ( status )
                
            {
                    rb_eval_string_protect(
            "print $!"&status);
                    
            return false;
                }


                
            return true;
            }


            bool RubyInterpreter::executeFile(String rubyfile)
            {
                
            bool error = execute("load '" + rubyfile + "'");
                
            return error;
            }



            SWIG的使用
            步驟大致為:
            1. 編寫后綴為.i的腳本;
            2. 使用swig生成導(dǎo)出代碼,假如腳本名為:sample.i,那么生成的源碼文件名規(guī)則就為:sample_wrap.cpp/.c.
            3. 將生成的cpp加入動(dòng)態(tài)鏈接庫,然后編譯.

            最簡(jiǎn)單的.i腳本為:
            %module Export4ScriptLib
            %{
            #include 
            "Player.h"
            %}



            %include "stl.i"
            %include "Player.h"
            Edit:如果想要使用STL的導(dǎo)出類,那就需要添加%include "stl.i"
            假如說,頭文件里面定義的所有的類,類所有的方法,你都要將之導(dǎo)出,那么以上就足夠了.但是,假如你只需要導(dǎo)出部分的類,部分的類的方法.那么你就需要自己手動(dòng)寫入到.i腳本里面去了.

            生成代碼的命令為:
            swig.exe -c++ -ruby Exports.i
            這樣寫的前提是你已經(jīng)吧swig的路徑加入到環(huán)境變量里面去了,其中第一個(gè)參數(shù)表示的是導(dǎo)出的代碼為c++,第二個(gè)參數(shù)表示的目標(biāo)腳本語言是誰,第三個(gè)參數(shù)是.i腳本的路徑名.我寫了一個(gè)批處理:invoke_swig.bat,做這件事情.不過更完美的做法是在VC項(xiàng)目里面的"預(yù)生成事件"加入此語句.

            剩下的事情就是把生成的代碼和要導(dǎo)出的代碼編譯一邊,就可以開始使用導(dǎo)出的C++庫了.


            測(cè)試
            在實(shí)例代碼里面:Export4ScriptLib工程是動(dòng)態(tài)鏈接庫工程,testRubyInterpreter是測(cè)試用的可執(zhí)行程序工程.
            測(cè)試用的Ruby代碼test.rb如下:
            require 'Export4ScriptLib'

            print "hello 你好!\n"


            ply 
            = Export4ScriptLib::Player.new
            ply.Jump();
            ply.Move(
            1002000);
            測(cè)試用C++代碼如下:
            class testClient
            {
            public:
                testClient()
                {
                    mRubyInterpreter 
            = new RubyInterpreter();
                    mRubyInterpreter
            ->initializeInterpreter();
                }
                
                
            ~testClient()
                {
                    delete mRubyInterpreter;
                }

                void 
            exec()
                {
                    
            // 執(zhí)行語句
                    mRubyInterpreter
            ->execute("print \"This is C++ call Ruby print funtion!\n\"");
                    
                    
            // 執(zhí)行文件
                    mRubyInterpreter
            ->executeFile("test.rb");
                }

            private:
                RubyInterpreter
            * mRubyInterpreter;
            };



            源代碼下載
            testRubyInterpreter.rar


            參考資料
            1. 什么是Ruby http://www.kuqin.com/beginner/ruby.html
            2. SWIG http://swig.minidx.com/
            3.  http://raylinn.javaeye.com/blog/629329

            posted on 2010-06-08 21:49 楊粼波 閱讀(7662) 評(píng)論(8)  編輯 收藏 引用 所屬分類: 原創(chuàng)文章

            評(píng)論

            # re: C++程序中嵌入Ruby腳本系統(tǒng) 2010-06-08 23:03 陳梓瀚(vczh)

            我現(xiàn)在正在開發(fā)的vlpp使用的方法是

            BasicFunctionExecutor<int(int*, int)> sum=vm.get(compile(code),...);

            int ns[]={1,2,3,4,5};
            int s=sum(ns, 5);  回復(fù)  更多評(píng)論   

            # re: C++程序中嵌入Ruby腳本系統(tǒng)[未登錄] 2010-06-08 23:13 楊粼波

            這個(gè)類只是用來驗(yàn)證和講解用的,
            沒有必要做那么復(fù)雜的設(shè)計(jì),那是會(huì)讓人閱讀上有些困難的.
            任何東西并不是一味的復(fù)雜就是好的.  回復(fù)  更多評(píng)論   

            # re: C++程序中嵌入Ruby腳本系統(tǒng) 2010-06-08 23:23 陳梓瀚(vczh)

            @楊粼波
            實(shí)現(xiàn)復(fù)雜沒關(guān)系,用起來要簡(jiǎn)單。接口要做好,但是完全沒必要因?yàn)閯e人看不懂而扭曲自己的設(shè)計(jì)。那只是implementation。  回復(fù)  更多評(píng)論   

            # re: C++程序中嵌入Ruby腳本系統(tǒng)[未登錄] 2010-06-08 23:31 楊粼波

            --!這也不是扭曲自己的設(shè)計(jì).
            因?yàn)槿绻粋€(gè)東西是給人用的,那接口簡(jiǎn)單明了就OK了.
            但是要是分解其原理,
            那就不必要做復(fù)雜了.
            因?yàn)榻涌诒旧砭褪菫榱似帘螐?fù)雜,
            而我現(xiàn)在就是要展示接口背后的東西,
            顯然的需要化復(fù)雜為簡(jiǎn)單了^_^  回復(fù)  更多評(píng)論   

            # re: C++程序中嵌入Ruby腳本系統(tǒng) 2010-06-09 11:45 mega

            vczh是要你夸夸他呢 哈哈  回復(fù)  更多評(píng)論   

            # re: C++程序中嵌入Ruby腳本系統(tǒng)[未登錄] 2010-06-09 11:47 楊粼波

            --!這樣子?
            汗,是我不解風(fēng)情了?
            呃.....  回復(fù)  更多評(píng)論   

            # re: C++程序中嵌入Ruby腳本系統(tǒng) 2010-06-09 14:00 陳梓瀚(vczh)

            @mega
            nono,要傳播知識(shí)。  回復(fù)  更多評(píng)論   

            # re: C++程序中嵌入Ruby腳本系統(tǒng) 2010-06-09 14:01 楊粼波

            中國急需要性教育,哈哈哈.....  回復(fù)  更多評(píng)論   

            久久99国产精品久久99果冻传媒| 2019久久久高清456| 亚洲va中文字幕无码久久| 国产午夜精品久久久久九九| 久久精品国产亚洲av水果派 | 久久久久国产一区二区三区| 国产精品对白刺激久久久| 亚洲国产欧美国产综合久久| 久久人与动人物a级毛片| 中文字幕精品久久久久人妻| 综合久久给合久久狠狠狠97色| 久久久黄片| 久久香蕉国产线看观看猫咪?v| 国产综合精品久久亚洲| 狠狠综合久久综合中文88| segui久久国产精品| 久久国产成人| 无码人妻少妇久久中文字幕| 亚洲欧洲精品成人久久曰影片| 久久丝袜精品中文字幕| 亚洲国产成人久久综合区| 国产精品久久久久蜜芽| 久久人妻无码中文字幕| 亚洲色大成网站www久久九| 久久这里只有精品18| 久久精品水蜜桃av综合天堂| 国产欧美久久一区二区| 99久久国产综合精品网成人影院| 国产精品成人99久久久久 | 久久97久久97精品免视看秋霞| 久久成人精品| 亚洲第一永久AV网站久久精品男人的天堂AV | 久久青青草原精品国产| 嫩草影院久久99| 欧美麻豆久久久久久中文| 久久精品国产久精国产果冻传媒 | 久久精品国产网红主播| 99精品久久久久久久婷婷| 亚洲精品无码久久久| 久久偷看各类wc女厕嘘嘘| 国产午夜精品理论片久久|