# Extending ruby with C++

**URL:** <https://www.ruby-forum.com/t/extending-ruby-with-c/132486>\
**Category:** Ruby\
**Created:** [March 20, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486 "2008-03-20T17:42:21Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 20, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/1 "2008-03-20T17:42:21Z")

</div>

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

[http://www.angelfire.com/electronic2/issac/rb\_cpp\_ext\_tut.txt](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?

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 20, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/2 "2008-03-20T20:30:41Z")

</div>

7stud – wrote:

> I’m using this old (2001) mini tutorial:
> 
> [http://www.angelfire.com/electronic2/issac/rb\_cpp\_ext\_tut.txt](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

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 20, 2008, 11:16pm UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/3 "2008-03-20T23:16:26Z")

</div>

Can’t help you with that specific problem, but i can recommend you rice  
([http://rice.rubyforge.org/](http://rice.rubyforge.org/)) for building ruby extensions with c++.

Should be easier than using a 7 year old tutorial 😉

best,  
marc

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 21, 2008, 2:11am UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/4 "2008-03-21T02:11:55Z")

</div>

Marc Dietrichstein wrote:

> Can’t help you with that specific problem, but i can recommend you rice  
> ([http://rice.rubyforge.org/](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.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 21, 2008, 2:32am UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/5 "2008-03-21T02:32:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 21, 2008, 2:07am UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/6 "2008-03-21T02:07:12Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 21, 2008, 2:54am UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/7 "2008-03-21T02:54:07Z")

</div>

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.

??

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 21, 2008, 2:55am UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/8 "2008-03-21T02:55:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 21, 2008, 11:18am UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/9 "2008-03-21T11:18:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 21, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/10 "2008-03-21T18:33:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 22, 2008, 3:33am UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/11 "2008-03-22T03:33:45Z")

</div>

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

Ron.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 22, 2008, 10:53am UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/12 "2008-03-22T10:53:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 22, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/13 "2008-03-22T21:18:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 22, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/14 "2008-03-22T14:23:35Z")

</div>

$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

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 23, 2008, 12:07am UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/15 "2008-03-23T00:07:26Z")

</div>

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:  
> > [Make (software) - Wikipedia](http://en.wikipedia.org/wiki/Make_%28software%29)

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

– Bill

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 22, 2008, 9:23pm UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/16 "2008-03-22T21:23:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [March 24, 2008, 12:03am UTC](https://www.ruby-forum.com/t/extending-ruby-with-c/132486/17 "2008-03-24T00:03:32Z")

</div>

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