473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using C++ functions from C

MSG
Hello

The C++ standard provides a way to access C function, which is `extern
"C"`, however, what about the compatibility in the opposite direction?

If my C++ function happens to have the signature of a valid C
function, are there guarantees about its usability from C?

Thanks
MSG
Jul 22 '05 #1
6 1153


MSG wrote:
Hello

The C++ standard provides a way to access C function, which is `extern
"C"`, however, what about the compatibility in the opposite direction?

If my C++ function happens to have the signature of a valid C
function, are there guarantees about its usability from C?

Thanks
MSG


I haven't investigated it thoroughly, but try enclose your C++ functions
within extern "C" {} as well. Of course it has to be static functions,
but they in turn can use C++ fully.

Regards
Daniel Marcus


Jul 22 '05 #2
On 9 Feb 2004 02:48:31 -0800 in comp.lang.c++, ms*****@yahoo.com (MSG)
was alleged to have written:
The C++ standard provides a way to access C function, which is `extern
"C"`, however, what about the compatibility in the opposite direction?

If my C++ function happens to have the signature of a valid C
function, are there guarantees about its usability from C?


Not "just happens to" but you have declared it have "C" linkage with
extern "C"

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[32.6] How can I create a C++ function f(int,char,float) that is
callable by my C code?" It is always good to check the FAQ before
posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Jul 22 '05 #3
MSG,

If you need an example project which accomplishes this task, drop me a line.

Evan Carew

MSG wrote:
Hello

The C++ standard provides a way to access C function, which is `extern
"C"`, however, what about the compatibility in the opposite direction?

If my C++ function happens to have the signature of a valid C
function, are there guarantees about its usability from C?

Thanks
MSG

Jul 22 '05 #4
DeMarcus <no****@tellus.orb> wrote in message news:<40**************@tellus.orb>...
MSG wrote:
Hello

The C++ standard provides a way to access C function, which is `extern
"C"`, however, what about the compatibility in the opposite direction?

If my C++ function happens to have the signature of a valid C
function, are there guarantees about its usability from C?

Thanks
MSG


I haven't investigated it thoroughly, but try enclose your C++ functions
within extern "C" {} as well. Of course it has to be static functions,
but they in turn can use C++ fully.


Hmm...I'm unsure as to the exact meaning of extern "C" { static void
foo(); } - you're telling the compiler two things at once (firstly
that you want it to have external "C"-style linkage, secondly that you
want it not to be visible outside the current compilation unit). Note
that extern "C" is not valid on a struct/class member, even if it is
'static'. And

extern "C" static void foo();

is not a valid declaration.

Dylan
Jul 22 '05 #5


Dylan Nicholson wrote:
DeMarcus <no****@tellus.orb> wrote in message news:<40**************@tellus.orb>...
MSG wrote:
Hello

The C++ standard provides a way to access C function, which is `extern
"C"`, however, what about the compatibility in the opposite direction?

If my C++ function happens to have the signature of a valid C
function, are there guarantees about its usability from C?

Thanks
MSG


I haven't investigated it thoroughly, but try enclose your C++ functions
within extern "C" {} as well. Of course it has to be static functions,
but they in turn can use C++ fully.

Hmm...I'm unsure as to the exact meaning of extern "C" { static void
foo(); } - you're telling the compiler two things at once (firstly
that you want it to have external "C"-style linkage, secondly that you
want it not to be visible outside the current compilation unit). Note
that extern "C" is not valid on a struct/class member, even if it is
'static'. And

extern "C" static void foo();

is not a valid declaration.

Dylan

I made a small program like this

class MyCppClass
{
public:
char* helloWorldMessage( void ) { return "Hello World"; }
};

extern "C"
{

char* foo( void )
{
MyCppClass cpp;

return cpp.helloWorldMessage();
}

}

It seems to work, but I'm curious in what the common way
to solve this task is.

Daniel
Jul 22 '05 #6
You cannot do that as a C++ method maps to a C function with an additional
pointer that points to the object.

See the following article for details:

http://www.eventhelix.com/RealtimeMa...erformance.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Distributed System Design CASE Tool
Jul 22 '05 #7

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

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
4
by: Christian Christmann | last post by:
Hi, I'm reading "C++ Coding Standards" by Herb Sutter. On page 67 there's an example which I don't understand: --------------------------------- class Base{// ... virtual void Foo(int);...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
2
by: Henry | last post by:
Hi guys, I want to write some global functions which can be called from different asp.net page. In Visual Basic, there is a global module which allow me to do that. In Visual Basic .net, I...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
5
by: serge | last post by:
Is it generally or almost always better to have multiple small SPs and functions to return a result set instead of using a single big 1000+ lines SP? I have one SP for example that is 1000+...
39
by: Mark Odell | last post by:
I've always declared variables used as indexes into arrays to be of type 'size_t'. I have had it brought to my attention, recently, that size_t is used to indicate "a count of bytes" and that using...
12
by: BDowling | last post by:
Hi all, I have an existing Visual C++ project that builds a DLL (not COM or ATL or anything). I wish to use this DLL in a Visual Studio 2005 C# project without having to define each function and...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
3
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.