• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

[Tutorial] SketchUp Ruby C Extension

Scheduled Pinned Locked Moved Developers' Forum
176 Posts 16 Posters 25.9k Views 16 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    Anton_S
    last edited by 6 Aug 2012, 19:37

    Lets, say I compile a HelloWorldEx on windows7, will it be the same as compiling it on XP?

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 6 Aug 2012, 20:08

      I would think it would depend upon which MSVC runtime you use.

      Which ever you use, the user(s) will need to have that version installed on their machine(s), regardless of whether they are running Win5.1 (XP,) Win6.0 (Vista) or Win6.1 (Win7.)

      SketchUp itself lists MSVC ver 8.x (2005) as a dependent assembly in it's manifest.

      There ARE some programmers that have compiled extensions and linked to ver 10 (2010), but they must give the Microsoft download link to install that runtime version, and list it as a dependancy.

      Reference:
      http://en.wikipedia.org/wiki/Visual_C++

      πŸ’­

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • A Offline
        Anton_S
        last edited by 7 Aug 2012, 03:23

        @dan rathbun said:

        Which ever you use, the user(s) will need to have that version installed on their machine(s), regardless of whether they are running Win5.1 (XP,) Win6.0 (Vista) or Win6.1 (Win7.)

        Hmm, I have win7 and windowns XP mode virtual pc , on the same comp.

        Under Win6.1(win7), I recompiled win32/api.so to my own namespace with a name changed to win32/msp_api.so. ( MSPhysics::Win32::API)
        It worked pretty well, until I've desided test it on WinXP mode virtual pc. WinXPmode had ruby-186-27, SU8, and some more raw programs, though it did not have MSVC.
        Then, I started SU 8 and got this error:
        msp_api.so without msft visual c++

        Before I installed msvc 2010, I desided to test the native win32/api.so and seeing it working wasn't expected; Apperently the native win32/api.so worked pretty well, without showing any errors, and without msvc.

        Being a little confused I desided to download msvc 2010 Redistributable Package (x86) , to test whether my win32/msp_api.so would actually work on XPmode. After downloading, it worked now, but there was one question left: How did native win32/api.somanaged to work without msvc? ❓

        1 Reply Last reply Reply Quote 0
        • T Offline
          thomthom
          last edited by 7 Aug 2012, 07:44

          @dan rathbun said:

          There ARE some programmers that have compiled extensions and linked to ver 10 (2010), but they must give the Microsoft download link to install that runtime version, and list it as a dependancy.

          No - you don't have to link to the runtime.
          http://forums.sketchucation.com/viewtopic.php?f=180&t=41077&start=105#p411657

          After a simple change in a compiler flag I could use the .so without the runtime installed.

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

          1 Reply Last reply Reply Quote 0
          • D Offline
            Dan Rathbun
            last edited by 7 Aug 2012, 21:32

            @anton_s said:

            How did native win32/api.somanaged to work without msvc?

            Because the SketchUp Installer installs MSVC 8.x (2005) runtime if it is not installed. The MSVC runtime install, might seem transparent to the user installing SketchUp, if they do not pay close attention.

            Most XP machines will likely already have this older MSVC ".NET" runtime version installed if Automatic Windows Updates are turned ON.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • topic:timeago-later,23 days
            • D Offline
              Dan Rathbun
              last edited by 31 Aug 2012, 05:56

              (Ruby-Talk GoogleGroup) Common Traps for C extensions

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • topic:timeago-later,14 days
              • T Offline
                tomasz
                last edited by 14 Sept 2012, 09:25

                I am trying to build the Basic sample under Xcode 4.4, but I am probably doing something wrong, because the resulting bundle fails to load into SU. I get "#<LoadError: myplugin.bundle Failed to load myplugin.bundle". I do not even get the "Failed to find init.. " error.

                When compiling with extconf.rb and make the plugin loads just fine.

                What project type should I start with to get the extension running? I have already tied /Framework/Bundle, Generic C++ Plug-in. How can I force the -flat_namespace in Xcode?

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

                1 Reply Last reply Reply Quote 0
                • T Offline
                  thomthom
                  last edited by 14 Sept 2012, 09:30

                  I've never used XCode. :s
                  But if you find out, could you post back here? It'd be nice to include alternative setups.

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

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    Dan Rathbun
                    last edited by 14 Sept 2012, 16:38

                    I am sure that I remember an old thread where someone explained how to do a OSX extension. I think they said you had to link it to the framworkized Ruby that is under the SketchUp app directory (not the system Ruby that Apple installs.) .. If I recall correctly, that is.

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      thomthom
                      last edited by 14 Sept 2012, 17:12

                      I didn't have to relink anything. But I'm not familiar with his scenario of XCode 4.4.

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

                      1 Reply Last reply Reply Quote 0
                      • D Offline
                        Dan Rathbun
                        last edited by 14 Sept 2012, 17:18

                        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'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          thomthom
                          last edited by 14 Sept 2012, 17:25

                          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 Reply Last reply Reply Quote 0
                          • J Offline
                            Jim
                            last edited by 15 Sept 2012, 16:19

                            @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 Reply Last reply Reply Quote 0
                            • T Offline
                              tomasz
                              last edited by 17 Sept 2012, 13:28

                              @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 Reply Last reply Reply Quote 0
                              • T Offline
                                thomthom
                                last edited by 17 Sept 2012, 14:27

                                @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 Reply Last reply Reply Quote 0
                                • D Offline
                                  Dan Rathbun
                                  last edited by 17 Sept 2012, 14:41

                                  @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 Reply Last reply Reply Quote 0
                                  • T Offline
                                    tomasz
                                    last edited by 20 Sept 2012, 09:17

                                    @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 Reply Last reply Reply Quote 0
                                    • T Offline
                                      thomthom
                                      last edited by 24 Sept 2012, 08:12

                                      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 Reply Last reply Reply Quote 0
                                      • T Offline
                                        tomasz
                                        last edited by 25 Sept 2012, 07:08

                                        @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 Reply Last reply Reply Quote 0
                                        • T Offline
                                          tomasz
                                          last edited by 25 Sept 2012, 14:49

                                          @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 Reply Last reply Reply Quote 0
                                          • 1
                                          • 2
                                          • 5
                                          • 6
                                          • 7
                                          • 8
                                          • 9
                                          • 8 / 9
                                          8 / 9
                                          • First post
                                            147/176
                                            Last post
                                          Buy SketchPlus
                                          Buy SUbD
                                          Buy WrapR
                                          Buy eBook
                                          Buy Modelur
                                          Buy Vertex Tools
                                          Buy SketchCuisine
                                          Buy FormFonts

                                          Advertisement