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

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

On 4/13/07, [email protected] [email protected] 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

On Apr 13, 7:00 pm, [email protected] 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).