Extending ruby with C++

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 >::operator<<(int)
std::basic_ostream<char, std::char_traits >::operator<<(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 >& std::endl<char,
std::char_traits >(std::basic_ostream<char, std::char_traits

&)
std::basic_ostream<char, std::char_traits >& std::operator<<
<std::char_traits >(std::basic_ostream<char,
std::char_traits >&, 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?

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

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 :wink:

best,
marc

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 :wink:

best,
marc

Thanks for the response. I’ll take a look at Rice, too.

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++. “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

Thanks for the response.

Michael N. 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

Michael N. 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.

??

7stud – wrote:

Michael N. 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

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

Michael N. 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

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

Ron.

Ron F. 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.

Jason R. 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?

$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

7stud – wrote:

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

7stud – wrote:

Jason R. 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

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”)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs