Calling C++ methods from Plain C

S

sas

I know how to use extern "C" to call plain c methods from C++ files,
but how do you call methods in cpp files from c files? I need this
because the GUI oriented part of my program uses FLTK (C++), but the
main part is written in C.
 
S

Seebs

I know how to use extern "C" to call plain c methods from C++ files,
but how do you call methods in cpp files from c files? I need this
because the GUI oriented part of my program uses FLTK (C++), but the
main part is written in C.

Short answer: You may or may not be able to. And really, this is
fundamentally a C++ question, not a C question.

My guess is that if you declare C++ functions (not methods!) as extern "C",
you will be able to call them from C code.

-s
 
K

Keith Thompson

sas said:
I know how to use extern "C" to call plain c methods from C++ files,
but how do you call methods in cpp files from c files? I need this
because the GUI oriented part of my program uses FLTK (C++), but the
main part is written in C.

As it happens, it's C++, not C, that defines the mechanisms both for
calling C from C++ and for calling C++ from C.

See section 32 of the C++ FAQ Lite,
<http://www.parashift.com/c++-faq-lite/>. If you still have
questions, you'll get better answers in comp.lang.c++.
 
O

Old Wolf

I know how to use extern "C" to call plain c methods from C++ files,
but how do you call methods in cpp files from c files? I need this
because the GUI oriented part of my program uses FLTK (C++), but the
main part is written in C.

You have to write a plain-C function in the
cpp file. That function can call the cpp
functions, and your C code can call the
C function in the cpp file.

For more details, ask on comp.lang.c !
 
J

James Kuyper

Old said:
You have to write a plain-C function in the
cpp file. ...

I would say, more precisely, that you have to define a C++ function
whose type has extern "C" language linkage. I don't like to call it a
"C" function, even though "C" language linkage makes it callable from C.
That's because the body of the function will be compiled according to
the rules of C++, and can use any C++ feature you want it to use (which
is precisely what makes it useful for this purpose).

Even the declaration of the function in C++ code will be handled
according to the rules of C++, not the rules of C, which could cause
problems if you're not careful. Obviously the declaration of the
interface function cannot use any type that cannot be declared in C.
However, it's also a potential problem that a syntactically valid C
function declaration could be an invalid C++ function declaration, or
have a different meaning in in C++. The most important of the
differences that could cause problems in the declaration are the ones
involving the scope and name space of identifiers (not be confused with
C++ namespaces, which are a very different concept).
 
C

cognacc

I know how to use extern "C" to call plain c methods from C++ files,
but how do you call methods in cpp files from c files? I need this
because the GUI oriented part of my program uses FLTK (C++), but the
main part is written in C.

Check this out: Go down to.
Accessing C++ Code From Within C Source
http://developers.sun.com/solaris/articles/mixing.html

And read the rest also, gives a very good understanding of the problem
i think.

I used it to create a decoder in C and a graphical interface in C++
(QT).

Also think about wether you really need to make calls into the gui
code.
Shouldn't there be a separation?


mic
 

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

Forum statistics

Threads
474,303
Messages
2,571,559
Members
48,361
Latest member
arunkumar00
Top