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

D

Derek Haskin

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

Piers Harding

--4Y142/9l9nQlBiaj
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable


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.


=20
=20
=20
=20
Hi,
=20
Does any know how I would go about making calls to a set of APIs written = in
C++
=20
The reason is I want to use ruby to make direct calls to our source contr= ol
tool which is Harvest.
=20
thanks
derek.
=20
=20
=20

Privileged/Confidential information may be contained in this message.
If you are not the addressee indicated in this message (or responsible fo=
r 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 sen=
der 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 Int=
ernet e-mail for messages of this kind.
Opinions, conclusions and other information in this message that do not r=
elate to the official business of Transurban Limited and CityLink Melbourne=
Limited shall be understood as neither given nor endorsed by them.

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


--4Y142/9l9nQlBiaj
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCedjpGETamPT/o4ARApM4AJ420pJLReNFay+BJvRoW700GrrBRQCeMZdn
NTSQi7Majuk3hRUuZxg3pbQ=
=uMAK
-----END PGP SIGNATURE-----

--4Y142/9l9nQlBiaj--
 
N

Nikolai Weibull

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
 
P

Piers Harding

--5AmtYbcgdBTTPS58
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable


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 =3D 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.







Derek Haskin, May 5:
=20
Does any know how I would go about making calls to a set of APIs
written in C++
=20
You could search the archives at http://ruby-talk.org/ before asking
these questions. Anyway, here are two links with information,=20
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.
=20
[cut disclaimer]
=20
Do you really, and I mean really, need to include that disclaimer?,
nikolai
=20
--=20
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);}

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


--5AmtYbcgdBTTPS58
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCehCxGETamPT/o4ARAgXCAKCjECsuB7gftq/7LRR9TpB64UK4aACg+qP9
xt3DUoq5GxFNf7SIi0U+gSs=
=fGt5
-----END PGP SIGNATURE-----

--5AmtYbcgdBTTPS58--
 
P

Piers Harding

--wRRV7LY7NUeQGEoC
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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.


Piers Harding, May 5:
=20
OK - but here is a way to roll your own:
=E2=8B=AE
=20
I don't understand what this has to do with what I said,
nikolai
=20
--=20
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);}

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


--wRRV7LY7NUeQGEoC
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCehsbGETamPT/o4ARAjmdAJ0QELVOpJ5853X8TiBCp1aZh+v89wCeK9vC
t7eNoo+MJlXs8EaPKxwHiSo=
=IWj7
-----END PGP SIGNATURE-----

--wRRV7LY7NUeQGEoC--
 
N

Nikolai Weibull

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
 
G

Gennady Bystritksy

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

Edgardo Hames

[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."
 
N

Nikolai Weibull

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
 

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

Forum statistics

Threads
473,812
Messages
2,569,694
Members
45,478
Latest member
dontilydondon

Latest Threads

Top