Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

1 view
Skip to first unread message

Derek Haskin

unread,
May 4, 2005, 10:14:07 PM5/4/05
to

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

Piers Harding

unread,
May 5, 2005, 4:24:53 AM5/5/05
to

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.

--
http://www.piersharding.com
http://search.cpan.org/~piers/

Nikolai Weibull

unread,
May 5, 2005, 5:49:04 AM5/5/05
to
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
http://www.ruby-doc.org/docs/ProgrammingRuby/. 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);}


Piers Harding

unread,
May 5, 2005, 8:22:51 AM5/5/05
to

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 :-)

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


Cheers,

Piers Harding.

--
http://www.piersharding.com
http://raa.ruby-lang.org/search.rhtml?search=piers&search_target=owner
http://search.cpan.org/~piers/

Nikolai Weibull

unread,
May 5, 2005, 8:43:44 AM5/5/05
to
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,

Piers Harding

unread,
May 5, 2005, 9:07:18 AM5/5/05
to
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.

--

Nikolai Weibull

unread,
May 5, 2005, 10:32:07 AM5/5/05
to
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,

Gennady Bystritksy

unread,
May 5, 2005, 11:40:10 AM5/5/05
to
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? ;-)

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.

Edgardo Hames

unread,
May 5, 2005, 1:13:59 PM5/5/05
to
On 5/5/05, Nikolai Weibull

<mailing-lis...@rawuncut.elitemail.org> wrote:
>
> [cut disclaimer]
>
> Do you really, and I mean really, need to include that disclaimer?,
> nikolai
>

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

Kind Regards,
Ed
--
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."

Nikolai Weibull

unread,
May 5, 2005, 3:26:22 PM5/5/05
to
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,

0 new messages