# Compiling C++ module for Ruby

**URL:** <https://www.ruby-forum.com/t/compiling-c-module-for-ruby/74562>\
**Category:** Ruby\
**Created:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/compiling-c-module-for-ruby/74562 "2006-11-07T10:48:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_Chin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Albert\_Chin](https://www.ruby-forum.com/u/Albert_Chin)\
**Post date:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/compiling-c-module-for-ruby/74562/1 "2006-11-07T10:48:20Z")

</div>

What needs to change in extconf.rb to link against a C++ compiler? If  
I set CONFTEST\_C=“conftest.cxx”, then gcc is smart enough to build it  
with g++. However, that is not the case for C compilers from Sun, HP,  
IBM, etc. If Ruby was built with the vendor C compiler, what changes  
need to be made to extconf.rb so C++ tests use the corresponding  
vendor C++ compiler?

---

<div class="post-metadata">

**Author:** ![Albert\_Chin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Albert\_Chin](https://www.ruby-forum.com/u/Albert_Chin)\
**Post date:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/compiling-c-module-for-ruby/74562/2 "2006-11-07T10:48:22Z")

</div>

On Oct 31, 2006, at 9:05 AM, Albert Chin wrote:

> What needs to change in extconf.rb to link against a C++ compiler? If  
> I set CONFTEST\_C=“conftest.cxx”, then gcc is smart enough to build it  
> with g++. However, that is not the case for C compilers from Sun, HP,  
> IBM, etc. If Ruby was built with the vendor C compiler, what changes  
> need to be made to extconf.rb so C++ tests use the corresponding  
> vendor C++ compiler?

It is indeed a huge problem. I’ve found other way: I set CONFIG  
[“CPP”], then everything  
goes ok. But this hack breaks portability of extconf.rb

---

<div class="post-metadata">

**Author:** ![Albert\_Chin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Albert\_Chin](https://www.ruby-forum.com/u/Albert_Chin)\
**Post date:** [November 7, 2006, 10:50am UTC](https://www.ruby-forum.com/t/compiling-c-module-for-ruby/74562/3 "2006-11-07T10:50:07Z")

</div>

On Tue, Oct 31, 2006 at 03:05:05PM +0900, Albert Chin wrote:

> What needs to change in extconf.rb to link against a C++ compiler? If  
> I set CONFTEST\_C=“conftest.cxx”, then gcc is smart enough to build it  
> with g++. However, that is not the case for C compilers from Sun, HP,  
> IBM, etc. If Ruby was built with the vendor C compiler, what changes  
> need to be made to extconf.rb so C++ tests use the corresponding  
> vendor C++ compiler?

There is a rule in the generated makefile for building .cxx files using  
$(CXX). Ruby 1.9 sets this variable; earlier versions seem to rely on  
it getting set by make.

If CONFIG[‘CXX’] is unset, you could use find\_executable to find a  
binary with the same name as a common C++ compiler (autoconf seems to  
search $CCC g++ c++ gpp aCC CC cxx cc++ cl FCC KCC RCC xlC\_r xlC). Then  
open the Makefile for writing and add the line:

CXX=#{your\_cxx\_binary}

Paul

---

<div class="post-metadata">

**Author:** ![Albert\_Chin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Albert\_Chin](https://www.ruby-forum.com/u/Albert_Chin)\
**Post date:** [November 7, 2006, 10:55am UTC](https://www.ruby-forum.com/t/compiling-c-module-for-ruby/74562/4 "2006-11-07T10:55:08Z")

</div>

On Fri, Nov 03, 2006 at 12:15:13AM +0900, Albert Chin wrote:

> But that wouldn’t help run the C++ compiler during extconf.rb. How  
> would you run HP’s aCC C++ compiler to test for a symbol during  
> extconf.rb, for example?

You could redefine try\_link (or TRY\_LINK) and try\_compile, but that will  
use the C++ compiler for everything, which may not be what you want.

Paul

---

<div class="post-metadata">

**Author:** ![Albert\_Chin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@Albert\_Chin](https://www.ruby-forum.com/u/Albert_Chin)\
**Post date:** [November 7, 2006, 10:54am UTC](https://www.ruby-forum.com/t/compiling-c-module-for-ruby/74562/5 "2006-11-07T10:54:58Z")

</div>

Paul B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> it getting set by make.
> 
> If CONFIG[‘CXX’] is unset, you could use find\_executable to find a  
> binary with the same name as a common C++ compiler (autoconf seems to  
> search $CCC g++ c++ gpp aCC CC cxx cc++ cl FCC KCC RCC xlC\_r xlC). Then  
> open the Makefile for writing and add the line:
> 
> CXX=#{your\_cxx\_binary}

But that wouldn’t help run the C++ compiler during extconf.rb. How  
would you run HP’s aCC C++ compiler to test for a symbol during  
extconf.rb, for example?
