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

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

=== hello.h ===
#include <iostream>

#ifdef __cplusplus
extern "C" {
#endif

void hello_world() {
std::cout << "hello world";
}

#ifdef __cplusplus
}
#endif

Blessings,
TwP

···

On 4/13/07, james.d.masters@gmail.com <james.d.masters@gmail.com> wrote:

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 <iostream>
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?

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

···

On Apr 13, 7:00 pm, james.d.mast...@gmail.com 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?