Google Answers Logo
View Question
Q: Calling a member c++ function from a c file. ( Answered 5 out of 5 stars,   1 Comment )
Subject: Calling a member c++ function from a c file.
Category: Computers > Programming
Asked by: frankooo-ga
List Price: $5.00
Posted: 25 Nov 2003 23:40 PST
Expires: 25 Dec 2003 23:40 PST
Question ID: 280715
I have a Borland Builder C++ application that needs to communicate
back and forth with c files. It's easy enough to make c function calls
from C++ using extern, but I need to know how to easily do things in
the other direction.

For example, my c function has a need to set a particular serial port
pin which I need for it to be done through my C++ AsynchPro class
member object. I've done some reading, and it's not that hard to have
a c function call class static functions or access static variables,
but I need for it to access class member variables and/or functions.

Right now I'm faking it out by having the C file call a c++ static
function which sets a static variable indicating to a timing loop in
the c++ class that something has changed, which then goes ahead and
changes member objects/variables - in effect it's a message system.

However, this is growing bigger and I want to know how to hook these
calls in without a timer. I need a nicer solution. And no, I can't
just compile it all in either c or c++, i need to do it in this
specific way.


Request for Question Clarification by mathtalk-ga on 26 Nov 2003 08:01 PST
Hi, frankooo-ga:

You've probably done enough reading to learn that C++ compilers do
something called "name-mangling" for ordinary member functions, so
that the linker can verify the proper argument types for polymorphic

This is a bit similar to the use of underscores preceding the symbols
for C functions.  A "static" function in C, which basically means a
function private to a "file scope", is given a symbol without the
underscore.  Therefore the linker cannot associate its entry point
with calls from other files, because the external symbols generated in
other files always use the leading underscore.

So you are basically looking for a way to circumvent the protections
of C++ type checking.  I know you are finding it tiresome to create C
declaration functions for every new entry point that you want to call.
 I'd recommend an approach in which you try to reuse a single
"switchboard" C decl entry point, passing in some string that "routes"
the call where it needs to go, as a way of cutting down on the amount
of work needed to do this each time.  However it sounds as if you may
have played around with similar ideas and just want to know if the C++
member functions _can_ be called directly.

The answer is a compiler dependent one.  If you think a simple link or
one line paragraph (see the Google Answers guidelines for pricing)
would be satisfactory in response to your question, let us know.  It's
possible another Researcher may take a special interest in your
question and be willing to provide detailed instructions, but the
short answer is that you'll need to work with the linker settings and
symbol cross-reference files each time you want to establish a call
from C directly to a C++ member function.  I think you'll find this
even more tiresome than providing a "switchboard" entry point.

regards, mathtalk-ga

Clarification of Question by frankooo-ga on 26 Nov 2003 09:29 PST
I appreciate your descriptive explanation and your suggestion for a
workaround. In fact that does answer my question in that it's a very
difficult thing to do. I had mostly wanted to verify if I was missing
some easier way of doing things - eg passing in pointer to func,
etc... but it sounds as though it's a very messy thing to do, playing
with linkers isn't my idea of fun!

How do I award you with the answer, do you need to write back with a
dummy answer? Thanks.
Subject: Re: Calling a member c++ function from a c file.
Answered By: mathtalk-ga on 26 Nov 2003 10:13 PST
Rated:5 out of 5 stars
Hi, frankooo-ga:

Well, just for sport you might want to use the "create cross-reference
listing" in the C++ compiler for one of the modules.  Of course this
requires that you have the source code for it, which is one
limitation.  Then you identify the mangled name for the member
functions you need, which are then the forms you want to use from your
C code.

Playing with the linker, rather than letting the compiler/IDE tool
call it for you, is not much fun, but in days gone by there was more
of a collision between existing C code and new C++ stuff.

regards, mathtalk-ga
frankooo-ga rated this answer:5 out of 5 stars
Thanks! I'll close it now. 

Anyone who has time to provide a very brief ansert to a couple very
quick q, much appreciated: In Borland Builder, Project->Options, there
is a "Tasm" page which has an option to "generate listing", including
"cross reference", and "symbol tables information". I assume cross ref
will give me what I need. Is this listing typically generated inside
the obj file? If so, how do I view it? How precisely would I call a
function once I know the mangled name? Please provide a very quick
example? Finally, can I pass parameters, eg char, boolean in to
mangled functions? Thanks again

Subject: Re: Calling a member c++ function from a c file.
From: yosarian-ga on 27 Nov 2003 01:04 PST
Hi frankoo-ga.
The C++ faq lite is an excellent resource for this kind of things.
Here is a link to a similar question:

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy