linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* mixing C/C++
@ 2003-11-08  9:30 Elias Athanasopoulos
  2003-11-08 13:36 ` James Stevenson
  2003-11-10 15:00 ` Matthew Studley
  0 siblings, 2 replies; 19+ messages in thread
From: Elias Athanasopoulos @ 2003-11-08  9:30 UTC (permalink / raw)
  To: linux-c-programming

Hello!

I want to create Ruby bindings for a C++ project, so my first
step is to call C++ code from C, since Ruby has a plain C API.

Consider I have a Foo class which its implementation is compiled
in a shared lib (libtest.so). I have a second wrapper lib:

#include "libtest.h"

extern "C" class Foo *  wrap_foo_ctor() { return new Foo(); }
extern "C" void wrap_foo_set_food(Foo *f, int i) { f->set_food(i); }
extern "C" int  wrap_foo_hello(Foo *f) { return f->hello();  }

Using the above my C program is:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  struct Foo *m = (struct Foo*) wrap_foo_ctor();

  wrap_foo_set_food(m, 10);
  wrap_foo_hello(m);

  free(m);

  return 1;
}

And:

elathan@velka:~/src/bindings> gcc foo.c -Wall  -ltest -lwrap -o foo -L/home/elathan/src/bindings
foo.c: In function `main':
foo.c:6: warning: implicit declaration of function `wrap_foo_ctor'
foo.c:8: warning: implicit declaration of function `wrap_foo_set_food'
foo.c:9: warning: implicit declaration of function `wrap_foo_hello'
elathan@velka:~/src/bindings> ./foo
10


My main question is how to silent the implicit declaration warning in
gcc, which is, of course, correct. I want everything to compile with -Wall. 
Is there a warkaround? 

I tried to create a C header file, but I don't know how to make a C prototype
of:

 class Foo *  wrap_foo_ctor() { return new Foo(); }

Or silent the 'icompatible pointer type' warning in declarations, such as:

 int  wrap_foo_hello(Foo *f) { return f->hello();  }

TIA,
-- 
University of Athens			I bet the human brain 
Physics Department				is a kludge --Marvin Minsky 

	

^ permalink raw reply	[flat|nested] 19+ messages in thread
* mixing C/C++
@ 2003-11-24 11:10 Elias Athanasopoulos
  2003-11-24 12:18 ` Steven Shaw
  2003-11-24 18:44 ` Dari'o Mariani
  0 siblings, 2 replies; 19+ messages in thread
From: Elias Athanasopoulos @ 2003-11-24 11:10 UTC (permalink / raw)
  To: linux-c-programming

Hello!

Consider:

class Bar {
public:
  Bar();
  ~Bar();

  void dump2(void);
};

class Foo : public Bar {
public:
	Foo();
	~Foo();

	void dump(void);
};

extern "C"
Foo * wrap_foo_ctor()
{
	return new Foo();
}

extern "C"
void wrap_foo_dump(Foo *f)
{
	f->dump();
}

extern "C"
void wrap_bar_dump2(Bar *b)
{
	b->dump2();
}

Now, I have a C program:

int main(void)
{
	struct Foo *f;

	f = wrap_foo_ctor();
	wrap_foo_dump(f);
	wrap_bar_dump2(f);
}

which is working as -at least- I expect. Bar::dump2() is 
called since Foo inherits from Bar.

However is it correct? I have a larger C++ project, which I'm
creating wrappers for in C, and I get a segfault in a similar
case (i.e. when I call a method *deep* inside the inheritence tree of a 
class). 

Regards,
-- 
Elias Athanasopoulos
http://www.pcmag.gr - Libecom S.A.

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2003-12-02 18:22 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-11-08  9:30 mixing C/C++ Elias Athanasopoulos
2003-11-08 13:36 ` James Stevenson
2003-11-10 15:00 ` Matthew Studley
2003-11-10 17:37   ` Elias Athanasopoulos
2003-11-12  4:35     ` convert INT to CHAR but print's BEEP J.
2003-11-12  7:11       ` Mikael Aronsson
2003-11-13  1:26         ` convert INT to CHAR - SOLVED J.
2003-11-13  6:36       ` convert INT to CHAR but print's BEEP Jeff Woods
  -- strict thread matches above, loose matches on Subject: below --
2003-11-24 11:10 mixing C/C++ Elias Athanasopoulos
2003-11-24 12:18 ` Steven Shaw
2003-11-24 17:55   ` Elias Athanasopoulos
2003-11-25  2:21     ` Steven Shaw
2003-11-25 10:49       ` elathan
2003-11-25 11:42         ` Glynn Clements
2003-11-25 13:12           ` elathan
2003-11-26  2:09             ` Glynn Clements
2003-12-02 18:22               ` Elias Athanasopoulos
2003-11-25 16:32         ` Steven Shaw
2003-11-24 18:44 ` Dari'o Mariani

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).