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

J

james.d.masters

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
 
T

Tim Pease

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?

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
 
G

gga

% 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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,206
Latest member
SybilSchil

Latest Threads

Top