extending ruby with C++

7

7stud --

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 said:
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?
 
7

7stud --

Thanks for the response.

Michael said:
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
 
M

Michael Neumann

7stud said:
Thanks for the response.



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
 
7

7stud --

Michael said:
"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.

??
 
7

7stud --

7stud said:
How? Where? There is no mention of cc in extconf.rb.


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
 
R

Ron Fox

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'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 said:
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?
 
M

Michael Neumann

7stud said:
7stud said:
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
 
7

7stud --

Michael said:
7stud said:
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
 
J

Jason Roelofs

$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
 
7

7stud --

Jason said:
$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?
 
7

7stud --

7stud said:
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
 
J

Joshua Ballanco

7stud said:
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")
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top