469,140 Members | 1,386 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

Does this make sense? Calling C++ inside extern "C" block

I was told to look at some old C code that was ported to C++.

One of the file is like:

=========================================

CPPClass* someCPPVar = NULL;

extern "C"
{

void init()
{
someCPPVar = new CPPClass();
}

void somefunction()
{
bool (*callback)(...);
callback = NULL;

someCPPVar->someMethod((void (**)(...))&callback);

}
}

=========================================

My questions:
1. Can I remove the 'extern "C"' block as I have the source code?
I can just use a C++ compiler to compile it. Right?

2. Currently, I got compile error as follows: (Sun Forte Developer 7 C++ 5.4)

Error: Formal argument 1 of type void(*)(...)* in call to
CPPClass::someMethod(void(*)(...)*) is being passed extern
"C" void(*)(...)*.

Does anyone know how to resolve this problem?

Thanks in advance
Jul 22 '05 #1
1 2437


te*******@gmail.com wrote:
I was told to look at some old C code that was ported to C++.

One of the file is like:

=========================================

CPPClass* someCPPVar = NULL;

extern "C"
{

void init()
{
someCPPVar = new CPPClass();
}

void somefunction()
{
bool (*callback)(...);
callback = NULL;

someCPPVar->someMethod((void (**)(...))&callback);

}
}

this is typical 'thunk' code so that a C language module (or sometimes assembly
language) can indirectly
invoke a C++ class member function. It is sometimes used in embedded systems to
attach 'interrupts' to C++ member functions, but is also used to form a bridge
between C language modules and C++ language modules.

Mote that the 'extern "C"' does NOT say you can't use C++ code, what it says is
that the name mangling for the functions in its scope is going to conform to C
language usage rather than C++ language usage. What that implies indirectly is
that you can't have overloaded functions using the extern "C" linkage but that if
you have C language modules they can find the functions.

If you remove the extern "C" you may run into link errors down the road... but
that really depends on whether this code is still active or whether the original
reason it was written is obsoleted by further enhancements to the code (e.g. did
they go even further in turning this into native C++ code).

As far as the other issue you are seeing, it looks like the 'callback' variable
is being declared with extern "C" linkage but that since the class declaration
was done with C++ linkage there is a mismatch in the
linkage types. I'm not sure how to fix it... I'm not sure if I've seen a
compiler care as long as you don't
blatantly start assigning non-static class member functions to such variables...
I would say use 'cdecl' in the member function argument declaration but I'm not
100% sure that will fix it... or even if that is something that is part of the
standard and will be in your compiler. Another possibility is to move the
'callback' variable outside the 'extern "C"' block (make it global) but it
depends on exactly how much of this patch code there is to change... and *that*
may be an issue if the callbacks the code is trying to pass really do have C
language linkage... another thing that *may* work is to remove the global nature
of the extern "C" block and just qualify the individual functions as extern
"C"... one other idea is to put another level of thunking in, have the extern
"C" functions directly call C++ functions which declare the variables and do the
thunking up to the class methods.

Maybe someone else here will have other ideas or be able to tell you explicitly
what your compiler will accept.

David



Error: Formal argument 1 of type void(*)(...)* in call to
CPPClass::someMethod(void(*)(...)*) is being passed extern
"C" void(*)(...)*.

Does anyone know how to resolve this problem?

Thanks in advance

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

111 posts views Thread by JKop | last post: by
3 posts views Thread by Andy | last post: by
8 posts views Thread by Generic Usenet Account | last post: by
11 posts views Thread by Pete Wilson | last post: by
11 posts views Thread by L. Chen | last post: by
4 posts views Thread by kk_oop | last post: by
51 posts views Thread by Tony Sinclair | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.