Using C++ functions from C

M

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
 
D

DeMarcus

MSG said:
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
 
D

David Harmon

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/
 
E

Evan Carew

MSG,

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

Evan Carew
 
D

Dylan Nicholson

DeMarcus said:
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
 
D

DeMarcus

Dylan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top