Extensions in C++ (iostream)

Has anyone experience with or knows how to get iostream
working?

I have searched, but not found any results. I would
realy appreciate it.

Thanks in advance
Simon Strandgaard

here is my files

···

“test.rb”

require 'Test’
name = Test.getname
print “name=”,name,"\n"

// “test.cpp”
#include “ruby.h”
#include
//#include <stdio.h>

VALUE get_name(VALUE self) {
char *name = “hello world”;
cout << “test” << endl;
//printf(“test\n”);
return rb_str_new2(name);
}

typedef VALUE (*HOOK)();
extern “C” void Init_Test() {
VALUE test = rb_define_module(“Test”);
rb_define_module_function(test, “getname”, (HOOK)get_name, 0);
}

“extconf.rb”

require 'mkmf’
create_makefile(“Test”)

ruby/ext> ls
extconf.rb test.cpp test.rb
ruby/ext> ruby extconf.rb
creating Makefile
ruby/ext> make
c++ -fPIC -O -pipe -fPIC -I. -I/usr/local/lib/ruby/1.6/i386-freebsd4 -I. -c -o test.o test.cpp
cc -shared -Wl,-soname,Test.so -L/usr/local/lib -o Test.so test.o -L. -lruby -lc
ruby/ext> ruby test.rb
test.rb:1:in `require’: ./Test.so: Undefined symbol “endl__FR7ostream” - ./Test.so (LoadError)
from test.rb:1
ruby/ext>

Simon Strandgaard wrote:

cout << "test" << endl;

Agh! Use std::cout, std::endl etc or a “using namespace std”.
Namespaces are nice. (Not the cause of your problem though)

cc -shared -Wl,-soname,Test.so -L/usr/local/lib -o Test.so test.o -L. -lruby -lc

Doesn’t look like you are linking in the C++ standard library
explicitly.
To my knowledge, cc doesn’t do that implicitly. I think the switch is
-lstdc++ but I am not sure as I usually use the CXX (such as g++)
instead of CC for the linking, and I think that g++ links to the C++
library implicitly.

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

cc -shared -Wl,-soname,Test.so -L/usr/local/lib -o Test.so test.o -L. -lruby -lc

Change the call to `cc' by a call to `c++' i.e.

c++ -shared -Wl,-soname,Test.so -L/usr/local/lib ...

This is the option CONFIG["LDSHARED"], something like (to put in
extconf.rb)

   if CONFIG["LDSHARED"] == "cc -shared"
      CONFIG["LDSHARED"] = "c++ -shared"
   end

Guy Decoux

ruby/ext> g++ -shared -Wl,-soname,Test.so -L/usr/local/lib -o Test.so test.o -L. -lruby -lc
ruby/ext> ruby test.rb
test
name=hello world
ruby/ext>

It works… thanks

I would like to do this (in a nice way) from inside “extconf.rb” but how?

regards
Simon Strandgaard

···

Kent Dahl kentda@stud.ntnu.no wrote:

Simon Strandgaard wrote:

cc -shared -Wl,-soname,Test.so -L/usr/local/lib -o Test.so test.o -L. -lruby -lc

Doesn’t look like you are linking in the C++ standard library
explicitly.
To my knowledge, cc doesn’t do that implicitly. I think the switch is
-lstdc++ but I am not sure as I usually use the CXX (such as g++)
instead of CC for the linking, and I think that g++ links to the C++
library implicitly.

it does the trick, thanks alot. Im a happy man :wink:

Simon Strandgaard

···

On Tue, 15 Oct 2002 21:38:31 +0900 ts decoux@moulon.inra.fr wrote:

cc -shared -Wl,-soname,Test.so -L/usr/local/lib -o Test.so test.o -L. -lruby -lc

Change the call to cc' by a call to c++’ i.e.

c++ -shared -Wl,-soname,Test.so -L/usr/local/lib …

This is the option CONFIG[“LDSHARED”], something like (to put in
extconf.rb)

if CONFIG[“LDSHARED”] == “cc -shared”
CONFIG[“LDSHARED”] = “c++ -shared”
end