# Nuby question on using C++ with Swig - issues with cout

**URL:** <https://www.ruby-forum.com/t/nuby-question-on-using-c-with-swig-issues-with-cout/91983>\
**Category:** Ruby\
**Created:** [April 14, 2007, 12:05am UTC](https://www.ruby-forum.com/t/nuby-question-on-using-c-with-swig-issues-with-cout/91983 "2007-04-14T00:05:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 14, 2007, 12:05am UTC](https://www.ruby-forum.com/t/nuby-question-on-using-c-with-swig-issues-with-cout/91983/1 "2007-04-14T00:05:30Z")

</div>

I am looking into using Swig to bring some C++ libraries into Ruby.  
I’m not extremely C++ proficient, but know the basics. Anyway, I’m  
having issues with using “cout” within Swig. I’ve put together a  
small test case here:

=== hello.h ===  
#include   
void hello\_world() {  
std::cout \<\< “hello world”;  
}

=== hello.i ===  
%module example  
%{  
#include “hello.h”  
%}  
%include “hello.h”

=== extconf.rb ===  
require ‘mkmf’  
$libs = append\_library($libs, “supc++”)  
create\_makefile(‘hello’)

Commands:

% swig -c++ -ruby hello.i  
% ruby extconf.rb  
% make  
% ruby -e ‘require “hello”’  
./hello.so: ./hello.so: undefined symbol: \_ZSt4cout - ./hello.so  
(LoadError)  
from -e:1

Please help me on this… is this an issue with dynamic linking?

Thanks,  
-James

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 14, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/nuby-question-on-using-c-with-swig-issues-with-cout/91983/2 "2007-04-14T16:39:32Z")

</div>

On 4/13/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> create\_makefile(‘hello’)
> 
> Please help me on this… is this an issue with dynamic linking?

C++ name mangling problem. You need to put extern “C” around your  
method.

=== hello.h ===  
#include

#ifdef \_\_cplusplus  
extern “C” {  
#endif

void hello\_world() {  
std::cout \<\< “hello world”;  
}

#ifdef \_\_cplusplus  
}  
#endif

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 14, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/nuby-question-on-using-c-with-swig-issues-with-cout/91983/3 "2007-04-14T21:25:51Z")

</div>

On Apr 13, 7:00 pm, removed\_email\_address@domain.invalid wrote:

> % make  
> % ruby -e ‘require “hello”’  
> ./hello.so: ./hello.so: undefined symbol: \_ZSt4cout - ./hello.so  
> (LoadError)  
> from -e:1
> 
> Please help me on this… is this an issue with dynamic linking?

Yes. You need to link in the std C++ library (libstdc++ on linux).
