How to interface with an API written in C++?

Hi,

Does any know how I would go about making calls to a set of APIs written in
C++

The reason is I want to use ruby to make direct calls to our source control
tool which is Harvest.

thanks
derek.

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Privileged/Confidential information may be contained in this message.
If you are not the addressee indicated in this message (or responsible for delivery of the message to such person), you may not copy or deliver this message to anyone.
In such a case, you should destroy this message and kindly notify the sender by reply e-mail or by telephone on (03) 9612-6999 or (61) 3 9612-6999.
Please advise immediately if you or your employer does not consent to Internet e-mail for messages of this kind.
Opinions, conclusions and other information in this message that do not relate to the official business of Transurban Limited and CityLink Melbourne Limited shall be understood as neither given nor endorsed by them.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Hi - my only experience with this is with Perl, but I would take a guess
that the process should be similar (someone else my know better).

Basically you would create a normal C extension for Ruby, but ensure
that your symbols are externalised (extern "C" { ... }), and then use
the c++ compiler. Then create your subroutines as normal, and call out
to the C++ library functions (thatyou need to link in).
I've not tried it myself, but I would have thought that it could be made
to fly?

Cheers,

Piers Harding.

···

On Thu, May 05, 2005 at 11:14:07AM +0900, Derek Haskin wrote:

Hi,

Does any know how I would go about making calls to a set of APIs written in
C++

The reason is I want to use ruby to make direct calls to our source control
tool which is Harvest.

thanks
derek.

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Privileged/Confidential information may be contained in this message.
If you are not the addressee indicated in this message (or responsible for delivery of the message to such person), you may not copy or deliver this message to anyone.
In such a case, you should destroy this message and kindly notify the sender by reply e-mail or by telephone on (03) 9612-6999 or (61) 3 9612-6999.
Please advise immediately if you or your employer does not consent to Internet e-mail for messages of this kind.
Opinions, conclusions and other information in this message that do not relate to the official business of Transurban Limited and CityLink Melbourne Limited shall be understood as neither given nor endorsed by them.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

--

Derek Haskin, May 5:

Does any know how I would go about making calls to a set of APIs
written in C++

You could search the archives at http://ruby-talk.org/ before asking
these questions. Anyway, here are two links with information,
http://aeditor.rubyforge.org/ruby_cplusplus/index.html and
Programming Ruby: The Pragmatic Programmer's Guide. The first is C++
specific, the second contains information relating to API interfacing in
general.

[cut disclaimer]

Do you really, and I mean really, need to include that disclaimer?,
        nikolai

···

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

OK - but here is a way to roll your own:
take one C++ file called dohello.cpp:
extern "C" {
  void Init_dohello(void);
}
#include <ruby.h>
#include <iostream>

using namespace std;

int cpp_hello()
{
    cout << "Hello World!" << endl;
        return 0;
}

static VALUE my_hello( ) {
  cpp_hello();
  return Qtrue;
}

void
Init_dohello(void) {
        VALUE hello;
        hello = rb_define_module("CPPHello");
        rb_define_module_function(hello, "do_hello", ( VALUE (*)(...) ) my_hello, 0);
}

Take an extconf.rb file:
require 'mkmf'
have_library("stdc++")
create_makefile("dohello")

And a test.rb file:
require "dohello.so"
CPPHello.do_hello()

And stir to your liking :slight_smile:

P.S. this was done under Linux FC3 + Ruby 1.8.2

Cheers,

Piers Harding.

···

On Thu, May 05, 2005 at 06:49:04PM +0900, Nikolai Weibull wrote:

Derek Haskin, May 5:

> Does any know how I would go about making calls to a set of APIs
> written in C++

You could search the archives at http://ruby-talk.org/ before asking
these questions. Anyway, here are two links with information,
http://aeditor.rubyforge.org/ruby_cplusplus/index.html and
Programming Ruby: The Pragmatic Programmer's Guide. The first is C++
specific, the second contains information relating to API interfacing in
general.

[cut disclaimer]

Do you really, and I mean really, need to include that disclaimer?,
        nikolai

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

--

http://raa.ruby-lang.org/search.rhtml?search=piers&search_target=owner

Sometimes, those disclaimers are automatically added by the company
MTA and you have no way to avoid it.

Kind Regards,
Ed

···

On 5/5/05, Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

[cut disclaimer]

Do you really, and I mean really, need to include that disclaimer?,
       nikolai

--
Encontrá a "Tu psicópata favorito" http://tuxmaniac.blogspot.com

"Tener una amiga en Ginebra es como tener quinotos en almibar o uvas en ron."
"Programming is like sex... make one mistake, and support it the rest
of your life."
"Defeat is an accomplishment not even the best of us could achieve."

Piers Harding, May 5:

OK - but here is a way to roll your own:

I don't understand what this has to do with what I said,
        nikolai

···

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

Edgardo Hames, May 6:

Nikolai Weibull wrote:

> [cut disclaimer]

> Do you really, and I mean really, need to include that disclaimer?

Sometimes, those disclaimers are automatically added by the company
MTA and you have no way to avoid it.

That’s what the “and I mean really” nonessential clause was for,
        nikolai

···

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

It continues the conversation.

You came up with one set of suggestions based around rubyembed, and
Swing. I just demonstrated that you can do it without that if you
prefer not to go down that path.

Cheers,

Piers Harding.

···

On Thu, May 05, 2005 at 09:43:44PM +0900, Nikolai Weibull wrote:

Piers Harding, May 5:

> OK - but here is a way to roll your own:

I don't understand what this has to do with what I said,
        nikolai

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

--

http://raa.ruby-lang.org/search.rhtml?search=piers&search_target=owner

Piers Harding, May 5:

It continues the conversation.

Ah, not very obvious when you push down what I was saying to the bottom
of your message. Perhaps cut out only the vital parts of my message,
paste them at the top, and then write your reply?

You came up with one set of suggestions based around rubyembed, and
Swing. I just demonstrated that you can do it without that if you
prefer not to go down that path.

I assume you mean SWIG. Sure, that's a valid point; I've never really
liked SWIG myself,
        nikolai

···

--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

For a long time I've been doing C++/Ruby interfacing mannually. And about 2 weeks ago I got to giving SWIG a try (version 1.3.24).

I was truelly amazed with what I got. It transforms your C++ classes into Ruby classes practically seamlessly, even giving you opportunity to adjust to naming conventions simply by edditing a SWIG interface file. Included typemaps allow you, for example, return std::string from your C++ method, and automatically gets converted and returned to the Ruby world as Ruby string, without you doing a stir. Isn't it amazing? :wink:

However, you can still use ruby C API in your C++ methods if you need it. In particular, I use it to implement iterator-like methods using rb_is_block_given_p() and rb_yield and SWIG's SWIG_NewPointerObj(...) function.

After throwing away tons of now unnecessary code, I am fully convinced that SWIG is the way to go for C++/Ruby integration.

Gennady.

Nikolai Weibull wrote:

···

Piers Harding, May 5:

It continues the conversation.

Ah, not very obvious when you push down what I was saying to the bottom
of your message. Perhaps cut out only the vital parts of my message,
paste them at the top, and then write your reply?

You came up with one set of suggestions based around rubyembed, and
Swing. I just demonstrated that you can do it without that if you
prefer not to go down that path.

I assume you mean SWIG. Sure, that's a valid point; I've never really
liked SWIG myself,
        nikolai