473,687 Members | 3,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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((vo id (**)(...))&call back);

}
}

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

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::someM ethod(void(*)(. ..)*) is being passed extern
"C" void(*)(...)*.

Does anyone know how to resolve this problem?

Thanks in advance
Jul 22 '05 #1
1 2647


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((vo id (**)(...))&call back);

}
}

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::someM ethod(void(*)(. ..)*) is being passed extern
"C" void(*)(...)*.

Does anyone know how to resolve this problem?

Thanks in advance

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

111
6412
by: JKop | last post by:
Okay here we go, I feel it's about time people conversed about the bullshit aspects of C++ (including the bullshit stuff brought forward from C). I'll begin with a few of my own grievances: 1) The whole "function declaration Vs object definition" fiasco, which results in the following (temporary creating) syntax: Blah poo = Blah();
3
3000
by: Andy | last post by:
Hi, This is just a simple hello world program. #include <iostream> using namespace std; int main() { cout<<"Hello,world\n"; }
8
2180
by: Generic Usenet Account | last post by:
Our C++ program was linked with some legacy C functions. We had used the extern "C" declaration, and everything was working fine. Then someone thought that it would be better to have a consistent file naming convention and renamed the files with the legacy C functions from .c to .cxx. Lo and behold we started getting linker errors left and right. When we reverted back to the old naming convention, the problem went away. Also, the...
4
2762
by: cpptutor2000 | last post by:
Could some C++ guru help me please? I am trying to build an application using gcc 3.2.3, that has a some classes using functions defined in some C files in the same directory. If inside the C++ source file I use: extern "C"{ // C function declarations } //C++ code
11
4168
by: Pete Wilson | last post by:
Hi folks -- The page at http://www.pwilson.net/submit-demo.html will not validate. The validator at http://validator.w3.org tells me I can't have an input inside a form. Would some kind soul please tell me what I'm doing wrong?
11
8888
by: L. Chen | last post by:
The standard says that a char* or void* pointer has the least strict alignment. But I do not know what is a strict alignment. What does that mean?
4
4750
by: kk_oop | last post by:
Hi. I need to write a C++ callback function and register it with a C program. I've read that this can be done if the callback function is a static method. I've also read that I should use a global function with the extern "C" prefix. I was leaning toward using the static method approach until I saw a posting that said compatibility between static C++ functions and C is not guaranteed in the respective language standards. This made me...
51
3928
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and output to be within one directory, and I want to be able to combine files from different directories into another directory of my choice). My program seems to work fine, but I'm wondering about this loop: for (int i = 0; i < numFiles; i++)
3
1819
by: parag_paul | last post by:
Does the following typedef differ from a typedef inside a extern "C" #if defined(__cplusplus) extern "C" { #endif typedef int (*fun_point)(int);
0
8590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8783
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4321
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2961
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2214
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1947
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.