简体   繁体   中英

How I can access my C++ function in my C code or vice versa?

I want to implement a project in C , but it is comfortable to code some part of project in C++ and then call them from main C code.
Is it possible?! if yes, how I can do it?!
thanks in advance :)

PS
I used some libraries in my C++ Code such as OpenCV .

You'll need to "wrap" your C++ interface with regular C functions that take a parameter to indicate what object they'll be called on. For instance, if you have in C++

class A
{
    // .. boilerplate stuff...
    int SomeMethod(int n, float f);
};

Then along with it, you could declare a function such as

extern "C" int A_SomeMethod(void* Obj, int n, float f)
{
    return(((A*)Obj)->SomeMethod(n, f));
}

If you're not comfortable with the casting of the void*, you can implement some kind of map from an opaque handle to an A* . But the gist is you'll need to keep around some handle/pointer to the object that the method will be called on. In order to get the pointer/handle you'll need to wrap the allocation to:

extern "C" void* A_Instantiate()
{
    return new A;
}

The C++ files should be compiled separately along with the file with the functions above. A separate include for the C compilation should include declarations of all the functions above.

EDIT: The caveats and comments below are important; to answer the question, "Yes it is possible to call C++ from C", and this is one approach. It's not a complete approach as there isn't really a mechanistic way to do it, but it's a start. Also, don't forget to create another call-through for delete , etc, etc.

yes, you need to specify it as

extern "C"

this way it will make the function to have "C" linkage, then C code can call your function just as if it was in C. This function name will not be mangled then because C doesn't support overloading.

here let me cite @Faisal Vali:

  • extern "C" is a linkage-specification
  • Every compiler is required to provide "C" linkage
  • a linkage specification shall occur only in namespace scope
  • all function types, function names and variable names have a language linkage
  • two function types with distinct language linkages are distinct types even if otherwise identical
  • linkage specs nest, inner one determines the final linkage
  • extern "C" is ignored for class members
  • at most one function with a particular name can have "C" linkage (regardless of namespace)
  • extern "C" forces a function to have external linkage (cannot make it static)
  • Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languages is implementation-defined and language-dependent. Only where the object layout strategies of two language implementations are similar enough can such linkage be achieved

see Faisal Vali answer here

Q: Can I access my C code from C++ or vice versa?

A: Yes.

1) The main thing is to use extern "C" { ...} in all your headers to denote C-only functions and data, like this:

http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

/* Header file foo.h */
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif

/* These functions get C linkage */
void foo();

struct bar { /* ... */ };

#ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
}
#endif

2) The usual scenario is a C++ main program that calls a mix of C and C++ functions and structs. The structs and functions are all declared in headers, and all have "#ifdef __cplusplus/extern C".

3) Here is a good FAQ on mixing C and C++:

http://www.parashift.com/c++-faq/mixing-c-and-cpp.html

Unless strictly required , this is only for dyied-in-the-wool masochists. Doing it will require extreme care on both sides, and could well work today and explode spectacularly with the next compiler update. C++ requires a lot of runtime help, and getting that to work reliably from C isn't normally supported. You can call into C from C++, that is officially supported (and part of the standard, extern "C" and such).

Probably the best bet is to write your C in the subset handled by C and C++ (a starting point on the subtle differences is this ) and compile with the C++ compiler. Or get over it and decide what language you like most.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM