sketchucation logo sketchucation
    • 登入
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    ⚠️ Important | Libfredo 15.6b introduces important bugfixes for Fredo's Extensions Update

    [Tutorial] SketchUp Ruby C Extension

    已排程 已置頂 已鎖定 已移動 Developers' Forum
    176 貼文 16 Posters 30.3k 瀏覽 16 Watching
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • thomthomT 離線
      thomthom
      最後由 編輯

      Dunno - what OS are we talking about?

      I've only done this on OSX 10.4 and 10.5. Maybe the Ruby that shipped there was compatible with SketchUp's Ruby - and this new version isn't and might actually require you to link to SketchUp's libs? I'm just taking a stab in the dark here.

      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 條回覆 最後回覆 回覆 引用 0
      • J 離線
        Jim
        最後由 編輯

        @dan rathbun said:

        (Ruby-Talk GoogleGroup) Common Traps for C extensions

        [off:i2j9ll26]I prefer this interface to the Ruby forum:

        favicon

        (www.ruby-forum.com)

        I just don't care for the Google groups interface.[/off:i2j9ll26]

        Hi

        1 條回覆 最後回覆 回覆 引用 0
        • T 離線
          tomasz
          最後由 編輯

          @dan rathbun said:

          I'll have to find the old topic.

          Is v4.4 too new to work with such an old Ruby version as 1.8.5-p0 ??

          I don't think so.

          The "make" command uses same complier that is bundled with XCode , that is LLVM GCC 4.2.
          When the extension is compiled this way, it works fine in SU. It looks like it is just a matter of placing correct settings in proper Xcode windows.

          The "-dynamic -bundle -flat_namespace " flags go to 'Linker/Other Linker Flags'. I have chosen STL C++ Library template ... which produces a C++ dynamic shared library (.dylib). I guess it is right type. Unfortunately it does not load with require "extension". Something is still missing.

          Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

          1 條回覆 最後回覆 回覆 引用 0
          • thomthomT 離線
            thomthom
            最後由 編輯

            @unknownuser said:

            I have chosen STL C++ Library template ... which produces a C++ dynamic shared library

            Wait, are you making a C++ extension instead of C?

            I've seen some people making Ruby extensions in C++, but you need to perform some extra steps. Something like extern "C".

            http://rice.rubyforge.org/
            http://www.ruby-forum.com/topic/128668
            https://www.google.com/search?hl=en&client=firefox-a&hs=LXd&rls=org.mozilla:en-US:official&q=+site:ruby-forum.com+ruby+c+extension+c%2B%2B&sa=X&ei=7zJXUK62LvLV4QTAvIGgCQ&ved=0CFMQrQIwAw&biw=1600&bih=1096

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 條回覆 最後回覆 回覆 引用 0
            • Dan RathbunD 離線
              Dan Rathbun
              最後由 編輯

              @unknownuser said:

              I have chosen STL C++ Library template ... which produces a C++ dynamic shared library (.dylib). I guess it is right type. Unfortunately it does not load with require "extension". Something is still missing.

              It should according to the description of Kernel.require()

              (1) Try the whole filename: require("extension.dylib")

              (2) Try specifying Kernel.require("extension_name") instead of the copy of require() that is inherited by Object. (Some naughty plugin could have changed it.)

              (3) Be aware that require has undergone changes over the years, to both fix some bugs (early on it pushed filepaths into $LOADED_FEATURES before it was determined if the files loaded successfully,) and to change the iteration of paths in $LOAD_PATH (it used to iterate the $LOAD_PATH array multiple times for each filtype { .rb, .so, .o, .dll, ...etc.} This was changed later on so it only iterates each directory once.) One of the major SNAFUs with dealing with different Ruby versions for different SketchUp platforms and versions.

              (4) require() has ALWAYS first checked to see if the argument resolves to a valid absolute filepath, and loads that valid file, instead of iterating through the $LOAD_PATH array.

              I'm not here much anymore.

              1 條回覆 最後回覆 回覆 引用 0
              • T 離線
                tomasz
                最後由 編輯

                @thomthom said:

                Wait, are you making a C++ extension instead of C?

                Yes. I have finally made it. What a nightmare 😄

                Xcode

                New Project

                Template > "Command Line Tool"!

                Enter product name "SX_HelloWorld.bundle"

                Type "C++"

                The *.c file renamed to *.cpp.
                Init_SX_HelloWorld( void ) has to be enclosed in extern "C"

                extern "C" {
                // The init function here
                }
                

                The last line of the code should read:

                rb_define_module_function( mSUExtTest, "knock_knock", RUBY_METHOD_FUNC(rb_knock), 0 );
                

                Now the important thing

                Build Settings\Linking\Other Linker Flags > -dynamic -bundle -undefined suppress -flat_namespace

                Build Settings\Search Paths\Header Search Path > has to point to Ruby headers
                Build Settings\Search Paths\Library Search Path > has to point to Ruby compiled library

                I have compiled the extension with LLVM GCC 4.2

                Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                1 條回覆 最後回覆 回覆 引用 0
                • thomthomT 離線
                  thomthom
                  最後由 編輯

                  Where does this macro come from? RUBY_METHOD_FUNC

                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 條回覆 最後回覆 回覆 引用 0
                  • T 離線
                    tomasz
                    最後由 編輯

                    @thomthom said:

                    Where does this macro come from? RUBY_METHOD_FUNC

                    This sits in ruby.h. Without getting a proper pointer type the code will not compile, since it is C++ now.

                    #define RUBY_METHOD_FUNC(func) ((VALUE (*)(ANYARGS))func)
                    

                    Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                    1 條回覆 最後回覆 回覆 引用 0
                    • T 離線
                      tomasz
                      最後由 編輯

                      @dan rathbun said:

                      Is v4.4 too new to work with such an old Ruby version as 1.8.5-p0 ??

                      I have compiled the sample extension using dynamic library. My Lion has Ruby 1.8.7 present and this simple example worked just fine.

                      I am trying to compile Ruby 1.8.5 as a static library now and it looks that with Xcode 4.4, it is next to impossible to get things right. I wish I had Snow Leopard with Xcode 3.2.

                      Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                      1 條回覆 最後回覆 回覆 引用 0
                      • A 離線
                        Anton_S
                        最後由 編輯

                        When recompiling Win32::API under own namespace, do not use -Ox flag.
                        The -Ox flag from [Compiler Options](http://msdn.microsoft.com/en-US/library/19z1t1wy(v), which creates maximum optimization causes SketchUp to produce a bugsplat right after completion of callback function enumeration.

                        Example:

                        module My;;CallbackTest
                          EnumWindows = My;;Win32;;API.new('EnumWindows', 'KP', 'I', 'User32')
                          EnumWindowsProc = My;;Win32;;API;;Callback.new('IP', 'I'){ |hwnd, lParam|
                            puts hwnd
                            1 # continue process
                          }
                          
                          def self.listWindowHandles
                            EnumWindows.call(EnumWindowsProc, nil)
                            # With Compiled $CFLAGS = '-MT -Ox -W4'
                            #   SU crashes right after enumation through all parent handles.
                            # With Compiled $CFLAGS = '-MT -W4'
                            #   SU works fine, although the size looks ~1.5 KB greater than with the -Ox flag.
                          end
                        end
                        

                        When compiling Win32::API, I have $CFLAGS = '-MT -W4'

                        ...just sharing some Win32::API compilation experiments 🤓

                        1 條回覆 最後回覆 回覆 引用 0
                        • thomthomT 離線
                          thomthom
                          最後由 編輯

                          Good to know!

                          I should add some warning about the -Ox flag that it might cause problems - since it's very aggressive optimization.

                          Thomas Thomassen — SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

                          1 條回覆 最後回覆 回覆 引用 0
                          • thomthomT 離線
                            thomthom
                            最後由 編輯

                            Call for information

                            I´ve only used a Mac Mini PPC with 10.4 and a Mac Mini Intel with 105 when compiling C Extensions under OSX.

                            I´m getting all sorts of warning that 10.5 isn´t supported anymore, from Apple and the software. Fair enough.

                            But what I´d like to know is:
                            If I update to Montain Lion - what would I need to get SketchUp Ruby C Extensions running?
                            I take it that the OS Ruby versions is much newer now - and I´d have to link the build to something other than the OS Ruby?

                            Also, when compiling a C Extension under Mountain Lion - how backwards compatible will it be?

                            Thomas Thomassen — SketchUp Monkey & Coding addict
                            List of my plugins and link to the CookieWare fund

                            1 條回覆 最後回覆 回覆 引用 0
                            • AdamBA 離線
                              AdamB
                              最後由 編輯

                              @thomthom said:

                              I take it that the OS Ruby versions is much newer now - and I´d have to link the build to something other than the OS Ruby?

                              No, a Plugin resolves the symbols on load.

                              @thomthom said:

                              Also, when compiling a C Extension under Mountain Lion - how backwards compatible will it be?

                              As much as you ask for it to be: eg -mmacosx-version-min=10.4 will ensure it runs on 10.4 and up.

                              @thomthom said:

                              I should add some warning about the -Ox flag that it might cause problems - since it's very aggressive optimization.

                              Just to correct this. The optimization does not produce 'wrong code' per se. Its that it commonly will remove leaf function stack frames to increase performance, meaning Ruby's GC can't find a reference to a Object and it gets marked for collection. So ensure you keep references and you'll be fine.

                              Developer of LightUp Click for website

                              1 條回覆 最後回覆 回覆 引用 0
                              • thomthomT 離線
                                thomthom
                                最後由 編輯

                                @adamb said:

                                No, a Plugin resolves the symbols on load.

                                Really? But if the OS Ruby is 1.9, and I compile without doing anything. Won't it be compiled for 1.9 only then? Isn't there completely different functions between 1.8 and 1.9?

                                @adamb said:

                                As much as you ask for it to be: eg -mmacosx-version-min=10.4 will ensure it runs on 10.4 and up.

                                Where does this flag go in the compile process? In the MAKEFILE? In the arguments of the make command?
                                (I'm very green to this.)

                                @adamb said:

                                Just to correct this. The optimization does not produce 'wrong code' per se. Its that it commonly will remove leaf function stack frames to increase performance, meaning Ruby's GC can't find a reference to a Object and it gets marked for collection. So ensure you keep references and you'll be fine.

                                Thanks for the clarification.

                                Thomas Thomassen — SketchUp Monkey & Coding addict
                                List of my plugins and link to the CookieWare fund

                                1 條回覆 最後回覆 回覆 引用 0
                                • thomthomT 離線
                                  thomthom
                                  最後由 編輯

                                  @designingcrime said:

                                  Hey guys, so I finally got a Ruby C extension to compile on both os x and windows and link curllib. I got a lot of help from Luis at the RubyInstaller group and ended up using RubyInstaller and DevKit to link curl and build using the gcc compiler included with devkit.
                                  Here is the extension. Its a modified version of Dana's sketchup downloader, but also included uploading, basic http authentication and ssl certificate checking(all through curl). I provided instructions on how to build on both os x and windows.

                                  https://github.com/sunglass/SunglassPlugins/tree/master/SG-SketchupHook/src/Connection%20Manager

                                  Thanks for all your help guys.

                                  Dana - Lets get your Sketchup downloader plugin working.

                                  Link is dead. 😞

                                  Thomas Thomassen — SketchUp Monkey & Coding addict
                                  List of my plugins and link to the CookieWare fund

                                  1 條回覆 最後回覆 回覆 引用 0
                                  • W 離線
                                    Whaat
                                    最後由 編輯

                                    Just wanted to thank Thomas and everyone else for the contributions in this thread. I tried to get Thomas' Hello World extension working and had no problems at all, thanks to his clear documentation.

                                    My next experiment will be to incorporate SketchUp SDK calls into the Ruby Extension. Has anyone done this? Is it relatively straightforward? Any 'gotchas' that you could warn me about? Thanks

                                    SketchUp Plugins for Professionals

                                    1 條回覆 最後回覆 回覆 引用 0
                                    • W 離線
                                      Whaat
                                      最後由 編輯

                                      Problems already...
                                      I have the SDK .h files installed in the same folder as 'SX_HelloWorld.c'. I'm not sure what to do with the binary files provided with the SDK.

                                      I tried to include just one header <slapi/model/model.h> file from the SDK and this was the result.

                                      Feeling clueless...


                                      test1.jpg

                                      SketchUp Plugins for Professionals

                                      1 條回覆 最後回覆 回覆 引用 0
                                      • tt_suT 離線
                                        tt_su
                                        最後由 編輯

                                        We're actually working on a Hello World example for Visual Studio and Xcode for compiling Ruby C/C++ Extensions for SketchUp. We can expand that (create a work or extra branch) with SLAPI configured as well.

                                        By the way can you post a Gist of your code? You got some syntax errors there that appear to not be related to linking to the correct headers.

                                        1 條回覆 最後回覆 回覆 引用 0
                                        • W 離線
                                          Whaat
                                          最後由 編輯

                                          @tt_su said:

                                          We're actually working on a Hello World example for Visual Studio and Xcode for compiling Ruby C/C++ Extensions for SketchUp. We can expand that (create a work or extra branch) with SLAPI configured as well.

                                          By the way can you post a Gist of your code? You got some syntax errors there that appear to not be related to linking to the correct headers.

                                          Interesting, I was just now doing some research into 'extconf.rb' and I'm starting to think my problem is related to not configuring the location of the .lib and .h files inside of 'extconf.rb' using the 'dir_config' and 'have_library' commands.

                                          BTW, the only difference between my code and your 'hello world' code on bitbucket is that I #included one the SLAPI .h files at the top.

                                          I had assumed (or hoped) that it would be properly linked since I put the files in the same location as the .c file that I was trying to compile. 👊

                                          It goes without saying that I am a complete noob at this.

                                          SketchUp Plugins for Professionals

                                          1 條回覆 最後回覆 回覆 引用 0
                                          • tt_suT 離線
                                            tt_su
                                            最後由 編輯

                                            Hi Dale

                                            I looked into this and found that you cannot use Visual Studio's nmake for this as it's C89 - and the SLAPI is written in C99.

                                            It would work if you install the DevKit at Ruby Windows Installer - though I'm still working out some minor quirks. We're preparing some Hello World examples to get started with Ruby C Extensions. Can you hang in there a little bit more?

                                            1 條回覆 最後回覆 回覆 引用 0
                                            • 1
                                            • 2
                                            • 3
                                            • 4
                                            • 5
                                            • 6
                                            • 7
                                            • 8
                                            • 9
                                            • 5 / 9
                                            • 第一個貼文
                                              最後的貼文
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement