Skip to contentSkip to conversation

mixing C/C++

DISCUSSION Elias Athanasopoulos 5 participants 11 messages Nov 24, 2003 – Dec 2, 2003

lore
Share & tools
lore
#0 lore raw

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,

Signature · 2 lines
Elias Athanasopoulos
http://www.pcmag.gr - Libecom S.A.
#1 lore raw
Quoted 10 lines · Elias Athanasopoulos

Now, I have a C program:

int main(void)

{
	struct Foo *f;

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

Looks fine to me.

#2 lore raw
Quoted 7 lines · Steven Shaw
Quoted 6 lines · Elias Athanasopoulos

Now, I have a C program:

int main(void)

{
	struct Foo *f;
Quoted 6 lines
Quoted 4 lines
	f = wrap_foo_ctor();
	wrap_foo_dump(f);
	wrap_bar_dump2(f);
}

Looks fine to me.

Apparently it is fine. The problem occures when I am wrapping a class which inherits from multiple classes with virtual functions.

Any workarounds?

Regards,

Signature · 2 lines
University of Athens			I bet the human brain 
Physics Department				is a kludge --Marvin Minsky 
#10 lore raw
In reply to Elias Athanasopoulos

Hello:
I think that the problem is in

extern "C"
Foo * wrap_foo_ctor()

{
	return new Foo();
}

In your C program you are using a C++ construct inside C, wich may work but the C compiler doesn't understand virtual functions so it might be confused there.

Quoted 56 lines · Elias Athanasopoulos

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,

#3 lore raw
In reply to Elias Athanasopoulos
Quoted 13 lines · Elias Athanasopoulos
Quoted 12 lines · Steven Shaw
Quoted 10 lines · Elias Athanasopoulos

Now, I have a C program:

int main(void)

{
	struct Foo *f;

	f = wrap_foo_ctor();
	wrap_foo_dump(f);
	wrap_bar_dump2(f);
}
Quoted 5 lines
Quoted 1 line

Looks fine to me.

Apparently it is fine. The problem occures when I am wrapping a class which inherits from multiple classes with virtual functions.

That should be fine too.

Quoted 1 line

Any workarounds?

Maybe you have C++ code with static members. If so you need to compile main() with a C++ compiler. Here is an excellent resource:

	http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

Also the C++ FAQ Lite main page now has a Google-based sitesearch box:

	http://www.parashift.com/c++-faq-lite/

Steve.

#4 lore raw

Quoting Steven Shaw <[email protected]>:

Quoted 10 lines · Elias Athanasopoulos
Quoted 3 lines

Apparently it is fine. The problem occures when I am wrapping a class which inherits from multiple classes with virtual functions.

That should be fine too.

Quoted 1 line

Any workarounds?

Maybe you have C++ code with static members. If so you need to compile

main()

Quoted 4 lines

with a C++ compiler. Here is an excellent resource:

	http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html 

This is what I read and came to the above conclusion.

It states (32.9):

You can safely access a C++ object's data from a C function if the C++ class:

Has no virtual functions (including inherited virtual functions)

So I guess that is a problem. I verified that with the attached source file. Compile the lib.cxx as a shared libfoo.so file and then try to call f->dump3() (f is struct *Foo).

Also, I don't have main() in my project since it is a shared library.

Thanks for your help.

Regards,
Elias

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

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

virtual void dump3(void);
};

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

virtual void dump2(void);
};

class Foo : public Bar, public Taz{
public:

	Foo();
	~Foo();

	void dump(void);
};

Foo::Foo()
{
}

void Foo::dump(void)

{
	printf("Foo Hello!\n");
}

Bar::Bar()
{
}

Bar::~Bar()
{
}

void Bar::dump2(void)

{
	printf ("Bar Hello\n");
}

Taz::Taz()
{
}

void Taz::dump3(void)

{
	printf ("Taz Hello!\n");
}

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();
}

extern "C"
void wrap_taz_dump3(Taz *t)

{
	t->dump3();
}
#5 lore raw
Quoted 16 lines · [email protected] wrote:
Quoted 6 lines
Quoted 3 lines

Apparently it is fine. The problem occures when I am wrapping a class which inherits from multiple classes with virtual functions.

That should be fine too.

Quoted 7 lines
Quoted 1 line

Any workarounds?

Maybe you have C++ code with static members. If so you need to compile main()

with a C++ compiler. Here is an excellent resource:

	http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html 

This is what I read and came to the above conclusion.

Quoted 7 lines

It states (32.9):

You can safely access a C++ object's data from a C function if the C++ class:

Has no virtual functions (including inherited virtual functions)

So I guess that is a problem.

Not for you. You aren't trying to access an object's data (member variables) from a C function. The only C function in your original example was main(), and that doesn't access an object's data. All of the other functions are C++ functions; some of them have C linkage (extern "C"), but they are still C++ functions.

Quoted 3 lines

I verified that with the attached source file. Compile
the lib.cxx as a shared libfoo.so file and then try to call f->dump3() (f is struct *Foo).

It works fine for me:

	$ g++ -c -Wall lib.cxx 
	lib.cxx:5: warning: `class Taz' has virtual functions but non-virtual 
	   destructor
	lib.cxx:13: warning: `class Bar' has virtual functions but non-virtual 
	   destructor
	lib.cxx:21: warning: `class Foo' has virtual functions but non-virtual 
	   destructor
	$ g++ -shared -o libfoo.so lib.o 
	$ gcc -c -Wall foo.c
	$ gcc foo.o -L. -lfoo
	$ LD_LIBRARY_PATH=. ./a.out 
	Foo Hello!
	Bar Hello
	$ cat foo.c
	extern void *wrap_foo_ctor(void);
	extern void wrap_foo_dump(void *);
	extern void wrap_bar_dump2(void *);
	extern void wrap_taz_dump3(void *);
	
	int main(void)
	{
		void *f;
	
		f = wrap_foo_ctor();
		wrap_foo_dump(f);
		wrap_bar_dump2(f);
	
		return 0;
	}
Quoted 1 line

Also, I don't have main() in my project since it is a shared library.

If you're getting a segfault, you are running a program, and that program has a main() function.

In any case, I'm not sure that the comment about compiling main() with a C++ compiler is applicable to using C++ shared libraries on Linux. There are plenty of shared libraries which are written in C++ but are usable from C programs without special treatment (GLU is probably the most common example).

Signature · 1 line
Glynn Clements <[email protected]>
#6 lore raw

Quoting Glynn Clements <[email protected]>:

Quoted 1 line

It works fine for me:

[snipped]

Quoted 5 lines
		wrap_foo_dump(f); 
		wrap_bar_dump2(f); 
	 
		return 0; 
	} 

Up to that point yes. But if you try to call dump3() (exactly with the code you have pasted) you wan't get "Taz Hello", but "Bar Hello". This strange behaviour will not happen, if you subistute the virtual methods with ordinary ones.

Regards,
Elias

#9 lore raw
In reply to [email protected]
Quoted 1 line · you

Also, I don't have main() in my project since it is a shared library.

Do make sure that you link with g++ (as Glynn did in his example). It will take care of a special linker script that collects all the static initialisations and I think there may be some magic functions that get collected for exception handling too.

If that fails why don't you post a complete minimal example that coredumps or just your original code that's coredumping if necessary.

Steve.

#7 lore raw
In reply to [email protected]
Quoted 11 lines · [email protected] wrote:
Quoted 1 line

It works fine for me:

[snipped]

Quoted 5 lines
		wrap_foo_dump(f); 
		wrap_bar_dump2(f); 
	 
		return 0; 
	} 

Up to that point yes. But if you try to call dump3() (exactly with the code you have pasted) you wan't get "Taz Hello", but "Bar Hello". This strange behaviour will not happen, if you subistute the virtual methods with ordinary ones.

Right.

You cannot treat "Foo*" and "Taz*" as equivalent within C.

You would have to have separate wrap_foo_dump3() and wrap_taz_dump3(), and pass only Foo* to the former and only Taz* to the latter.

With single inheritance, the subclass' data structure normally begins with the (sole) superclass' data structure, with any subclass-specific fields appended afterwards. With multiple inheritance, this isn't possible; the beginning of the subclass' data structure cannot simultaneously match those for multiple superclasses.

A C++ compiler will adjust the pointer when performing a narrowing cast, so it points directly at the superclass' data structure. A C compiler can't do this, as it doesn't know anything about classes.

This issue is touched upon at the bottom of §32.8 of the FAQ to which Steven linked:

	http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.8
Signature · 5 lines
Glynn Clements <[email protected]>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#8 lore raw
Quoted 3 lines · Glynn Clements
Quoted 3 lines

Up to that point yes. But if you try to call dump3() (exactly with the code you have pasted) you wan't get "Taz Hello", but "Bar Hello". This strange behaviour will not happen, if you subistute the virtual methods with ordinary ones.

Thank you both for your detailed replies. My apologies for my delayed answer.

Regards,

Signature · 2 lines
University of Athens			I bet the human brain 
Physics Department				is a kludge --Marvin Minsky