extending ruby with C++

Discussion in 'Ruby' started by 7stud --, Mar 20, 2008.

  1. 7stud --

    7stud -- Guest

    I'm using this old (2001) mini tutorial:

    http://www.angelfire.com/electronic2/issac/rb_cpp_ext_tut.txt

    When I run make, I get this error:

    $ make
    cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    -lobjc
    /usr/bin/ld: Undefined symbols:
    __Unwind_Resume
    std::basic_ostream<char, std::char_traits<char> >::eek:perator<<(int)
    std::basic_ostream<char, std::char_traits<char> >::eek:perator<<(unsigned
    long)
    std::ios_base::Init::Init()
    std::ios_base::Init::~Init()
    std::__throw_bad_alloc()
    std::__throw_length_error(char const*)
    std::cout
    std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
    std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>
    >&)

    std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<<
    <std::char_traits<char> >(std::basic_ostream<char,
    std::char_traits<char> >&, char const*)
    operator delete(void*)
    operator new(unsigned long)
    ___gxx_personality_v0
    collect2: ld returned 1 exit status
    make: *** [Test.bundle] Error 1

    $ ls
    Makefile Test.cpp Test.o extconf.rb

    I tried that on mac osx. Any idea what's wrong?
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 20, 2008
    #1
    1. Advertising

  2. 7stud -- wrote:
    > I'm using this old (2001) mini tutorial:
    >
    > http://www.angelfire.com/electronic2/issac/rb_cpp_ext_tut.txt
    >
    > When I run make, I get this error:
    >
    > $ make
    > cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    > -lobjc


    use c++ or add -lstdc++.

    c++ -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    -lobjc -lstdc++

    Regards,

    Michael
     
    Michael Neumann, Mar 20, 2008
    #2
    1. Advertising

  3. Marc Dietrichstein, Mar 20, 2008
    #3
  4. 7stud --

    7stud -- Guest

    Thanks for the response.

    Michael Neumann wrote:
    > 7stud -- wrote:
    >> I'm using this old (2001) mini tutorial:
    >>
    >> http://www.angelfire.com/electronic2/issac/rb_cpp_ext_tut.txt
    >>
    >> When I run make, I get this error:
    >>
    >> $ make
    >> cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    >> -lobjc

    >
    > use c++ or add -lstdc++.
    >


    I don't know what that means.


    > c++ -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    > -lobjc -lstdc++
    >


    Following the tutorial, the only commands I executed were:

    1) $ ruby extconf.rb

    2) $ make


    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 21, 2008
    #4
  5. 7stud --

    7stud -- Guest

    Marc Dietrichstein wrote:
    > Can't help you with that specific problem, but i can recommend you rice
    > (http://rice.rubyforge.org/) for building ruby extensions with c++.
    >
    > Should be easier than using a 7 year old tutorial ;)
    >
    > best,
    > marc


    Thanks for the response. I'll take a look at Rice, too.
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 21, 2008
    #5
  6. 7stud -- wrote:
    > Thanks for the response.
    >
    > Michael Neumann wrote:
    >> 7stud -- wrote:
    >>> I'm using this old (2001) mini tutorial:
    >>>
    >>> http://www.angelfire.com/electronic2/issac/rb_cpp_ext_tut.txt
    >>>
    >>> When I run make, I get this error:
    >>>
    >>> $ make
    >>> cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    >>> -lobjc

    >> use c++ or add -lstdc++.
    >>

    >
    > I don't know what that means.


    "cc" is usually used to compile C programs. Use the command-line tool
    "c++" (or "g++") instead for C++. "cc" will determine that you are
    compiling C++ code, but it will not add the standard C++ library (-lstdc++).

    Add the following line to your extconf.rb file:

    $LIBS << " -lstdc++"

    Before the call to "create_makefile"!

    Regards,

    Michael
     
    Michael Neumann, Mar 21, 2008
    #6
  7. 7stud --

    7stud -- Guest

    Michael Neumann wrote:
    > 7stud -- wrote:
    >>>> $ make
    >>>> cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    >>>> -lobjc
    >>> use c++ or add -lstdc++.
    >>>

    >>
    >> I don't know what that means.

    >
    > "cc" is usually used to compile C programs. Use the command-line tool
    > "c++" (or "g++") instead for C++.


    How? Where? There is no mention of cc in extconf.rb.

    >"cc" will determine that you are
    > compiling C++ code, but it will not add the standard C++ library
    > (-lstdc++).
    >
    > Add the following line to your extconf.rb file:
    >
    > $LIBS << " -lstdc++"
    >
    > Before the call to "create_makefile"!
    >


    I tried adding that line to extconf.rb before the create_makefile()
    line, but I get the same error.

    ??

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 21, 2008
    #7
  8. 7stud --

    7stud -- Guest

    7stud -- wrote:
    > Michael Neumann wrote:
    >> 7stud -- wrote:
    >>>>> $ make
    >>>>> cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    >>>>> -lobjc
    >>>> use c++ or add -lstdc++.
    >>>>
    >>>
    >>> I don't know what that means.

    >>
    >> "cc" is usually used to compile C programs. Use the command-line tool
    >> "c++" (or "g++") instead for C++.

    >
    > How? Where? There is no mention of cc in extconf.rb.
    >
    >>"cc" will determine that you are
    >> compiling C++ code, but it will not add the standard C++ library
    >> (-lstdc++).
    >>
    >> Add the following line to your extconf.rb file:
    >>
    >> $LIBS << " -lstdc++"
    >>
    >> Before the call to "create_makefile"!
    >>

    >
    > I tried adding that line to extconf.rb before the create_makefile()
    > line, but I get the same error.
    >
    > ??


    Here's the error:

    $ ruby extconf.rb
    creating Makefile

    $ make
    cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    -lobjc -lstdc++
    /usr/bin/ld: Undefined symbols:
    __Unwind_Resume
    collect2: ld returned 1 exit status
    make: *** [Test.bundle] Error 1


    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 21, 2008
    #8
  9. 7stud --

    Ron Fox Guest

    Try using g++ to link the code rather than cc. Using cc does not
    include the g++ run-time libraries in the link.

    Ron.

    7stud -- wrote:
    > I'm using this old (2001) mini tutorial:
    >
    > http://www.angelfire.com/electronic2/issac/rb_cpp_ext_tut.txt
    >
    > When I run make, I get this error:
    >
    > $ make
    > cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    > -lobjc
    > /usr/bin/ld: Undefined symbols:
    > __Unwind_Resume
    > std::basic_ostream<char, std::char_traits<char> >::eek:perator<<(int)
    > std::basic_ostream<char, std::char_traits<char> >::eek:perator<<(unsigned
    > long)
    > std::ios_base::Init::Init()
    > std::ios_base::Init::~Init()
    > std::__throw_bad_alloc()
    > std::__throw_length_error(char const*)
    > std::cout
    > std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
    > std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>
    >> &)

    > std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<<
    > <std::char_traits<char> >(std::basic_ostream<char,
    > std::char_traits<char> >&, char const*)
    > operator delete(void*)
    > operator new(unsigned long)
    > ___gxx_personality_v0
    > collect2: ld returned 1 exit status
    > make: *** [Test.bundle] Error 1
    >
    > $ ls
    > Makefile Test.cpp Test.o extconf.rb
    >
    > I tried that on mac osx. Any idea what's wrong?
     
    Ron Fox, Mar 21, 2008
    #9
  10. 7stud -- wrote:
    > 7stud -- wrote:
    >> Michael Neumann wrote:
    >> I tried adding that line to extconf.rb before the create_makefile()
    >> line, but I get the same error.
    >>
    >> ??

    >
    > Here's the error:
    >
    > $ ruby extconf.rb
    > creating Makefile
    >
    > $ make
    > cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    > -lobjc -lstdc++
    > /usr/bin/ld: Undefined symbols:
    > __Unwind_Resume
    > collect2: ld returned 1 exit status
    > make: *** [Test.bundle] Error 1


    Try this:

    $LIBS << " -lstdc++ -lc"

    Regards,

    Michael
     
    Michael Neumann, Mar 21, 2008
    #10
  11. 7stud --

    7stud -- Guest

    Michael Neumann wrote:
    > 7stud -- wrote:
    >> creating Makefile
    >>
    >> $ make
    >> cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    >> -lobjc -lstdc++
    >> /usr/bin/ld: Undefined symbols:
    >> __Unwind_Resume
    >> collect2: ld returned 1 exit status
    >> make: *** [Test.bundle] Error 1

    >
    > Try this:
    >
    > $LIBS << " -lstdc++ -lc"
    >
    > Regards,
    >
    > Michael



    Here is my current extconf.rb:

    --------
    require 'mkmf'

    if (/mswin32/ =~ PLATFORM)
    $CFLAGS+=" -GX " # allow exceptions
    end

    $LIBS << " -lstdc++ -lc"

    create_makefile("Test")
    --------

    Here is the result:

    $ ruby extconf.rb
    creating Makefile

    $ make
    cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    -lobjc -lstdc++ -lc
    /usr/bin/ld: Undefined symbols:
    __Unwind_Resume
    collect2: ld returned 1 exit status
    make: *** [Test.bundle] Error 1

    $ls
    Makefile Test.cpp Test.o extconf.rb

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 21, 2008
    #11
  12. 7stud --

    7stud -- Guest

    Ron Fox wrote:
    > Try using g++ to link the code rather than cc. Using cc does not
    > include the g++ run-time libraries in the link.
    >
    > Ron.


    I don't know how to do that.

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 22, 2008
    #12
  13. $CC = "g++"
    $CXX = $CC # for good measure.

    You should probably go looking around at tutorials on how to build C
    and C++ code before you go much farther into this project.

    Jason

    On Sat, Mar 22, 2008 at 5:53 AM, 7stud -- <> wrote:
    > Ron Fox wrote:
    > > Try using g++ to link the code rather than cc. Using cc does not
    > > include the g++ run-time libraries in the link.
    > >
    > > Ron.

    >
    > I don't know how to do that.
    >
    >
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Jason Roelofs, Mar 22, 2008
    #13
  14. 7stud --

    7stud -- Guest

    Jason Roelofs wrote:
    > $CC = "g++"
    > $CXX = $CC # for good measure.
    >
    > You should probably go looking around at tutorials on how to build C
    > and C++ code before you go much farther into this project.
    >
    > Jason


    I know how to compile C++ programs from the command line or using an
    IDE, but I don't know how to get extconf.rb to compile C++ programs or
    what make does.


    Where do those statements go? Inside extconf.rb?

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 22, 2008
    #14
  15. 7stud --

    7stud -- Guest

    7stud -- wrote:
    > Jason Roelofs wrote:
    >> $CC = "g++"
    >> $CXX = $CC # for good measure.
    >>
    >> You should probably go looking around at tutorials on how to build C
    >> and C++ code before you go much farther into this project.
    >>
    >> Jason

    >
    > I know how to compile C++ programs from the command line or using an
    > IDE, but I don't know how to get extconf.rb to compile C++ programs or
    > what make does.
    >
    >
    > Where do those statements go? Inside extconf.rb?


    My current extconf.rb file:

    require 'mkmf'

    if (/mswin32/ =~ PLATFORM)
    $CFLAGS+=" -GX " # allow exceptions
    end

    $LIBS << " -lstdc++ -lc"
    $CC =="g++"
    $CXX == $CC

    create_makefile("Test")

    The output:

    $ ruby extconf.rb
    creating Makefile

    $ make
    g++ -fno-common -g -Os -pipe -fno-common -pipe -fno-common -pipe
    -fno-common -I. -I/usr/lib/ruby/1.8/universal-darwin8.0
    -I/usr/lib/ruby/1.8/universal-darwin8.0 -I. -c Test.cpp
    cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    -lobjc -lstdc++ -lc
    /usr/bin/ld: Undefined symbols:
    __Unwind_Resume
    collect2: ld returned 1 exit status
    make: *** [Test.bundle] Error 1

    $ls
    Makefile Test.cpp Test.o extconf.rb
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 22, 2008
    #15
  16. 7stud -- wrote:
    > 7stud -- wrote:
    >
    >> Jason Roelofs wrote:
    >>
    >>> $CC = "g++"
    >>> $CXX = $CC # for good measure.
    >>>
    >>> You should probably go looking around at tutorials on how to build C
    >>> and C++ code before you go much farther into this project.
    >>>
    >>> Jason
    >>>

    >> I know how to compile C++ programs from the command line or using an
    >> IDE, but I don't know how to get extconf.rb to compile C++ programs or
    >> what make does.

    For an overview of "make" see:
    http://en.wikipedia.org/wiki/Make_(software)

    Good luck and watch out for the tab characters; they aren't just white
    space.

    -- Bill
     
    William Rutiser, Mar 22, 2008
    #16
  17. 7stud -- wrote:

    >> Where do those statements go? Inside extconf.rb?

    >
    > My current extconf.rb file:
    >
    > require 'mkmf'
    >
    > if (/mswin32/ =~ PLATFORM)
    > $CFLAGS+=" -GX " # allow exceptions
    > end
    >
    > $LIBS << " -lstdc++ -lc"
    > $CC =="g++"
    > $CXX == $CC
    >
    > create_makefile("Test")
    >
    > The output:
    >
    > $ ruby extconf.rb
    > creating Makefile
    >
    > $ make
    > g++ -fno-common -g -Os -pipe -fno-common -pipe -fno-common -pipe
    > -fno-common -I. -I/usr/lib/ruby/1.8/universal-darwin8.0
    > -I/usr/lib/ruby/1.8/universal-darwin8.0 -I. -c Test.cpp
    > cc -bundle -L"/usr/lib" -o Test.bundle Test.o -lruby -lpthread -ldl
    > -lobjc -lstdc++ -lc
    > /usr/bin/ld: Undefined symbols:
    > __Unwind_Resume
    > collect2: ld returned 1 exit status
    > make: *** [Test.bundle] Error 1
    >
    > $ls
    > Makefile Test.cpp Test.o extconf.rb


    I've run into this problem myself on OS X. For some reason, the mkmf
    library in Ruby 1.8 fails to properly realize that it should use g++
    instead of gcc or cc for C++ files. (Luckily Ruby 1.9 doesn't have this
    problem ATM.) You need to explicitly tell mkmf to use g++ like so:

    cpp_command('g++')

    There's also the arch flags issue to worry about (Ruby, by default,
    tries to build universal, but many libraries build only one arch on OS
    X). I've com up with a fix for that as well. Including it you're
    extconf.rb should look like:

    require 'mkmf'
    require 'rbconfig'

    if Config::CONFIG["arch"] =~ /universal-darwin/
    case `uname -smr`.chomp
    when "i386" : ENV['ARCHFLAGS'] = '-arch i386'
    when "ppc" : ENV['ARCHFLAGS'] = '-arch ppc'
    end
    cpp_command('g++') if RUBY_VERSION < '1.9'
    end

    if (/mswin32/ =~ PLATFORM)
    $CFLAGS+=" -GX " # allow exceptions
    end


    create_makefile("Test")
    --
    Posted via http://www.ruby-forum.com/.
     
    Joshua Ballanco, Mar 23, 2008
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Basile STARYNKEVITCH
    Replies:
    0
    Views:
    127
    Basile STARYNKEVITCH
    Sep 2, 2003
  2. DaZoner
    Replies:
    0
    Views:
    99
    DaZoner
    Oct 7, 2003
  3. DaZoner
    Replies:
    1
    Views:
    127
    Aleksei Guzev
    Oct 14, 2003
  4. Dominik Werder

    Learning extending ruby

    Dominik Werder, Apr 28, 2004, in forum: Ruby
    Replies:
    8
    Views:
    292
    Joel VanderWerf
    May 1, 2004
  5. Dominik Werder

    Extending ruby with crypto++

    Dominik Werder, Sep 8, 2004, in forum: Ruby
    Replies:
    13
    Views:
    270
    Elias Athanasopoulos
    Sep 12, 2004
Loading...

Share This Page