cancel
Showing results for 
Search instead for 
Did you mean: 

Call C++ function from C file

Iswarya
Associate II

I would like to call C++ function from C file. How to do that.

I have C++ function call Cpp_func() in test.cpp file. In test.h file, I declare as extern "C" Cpp_func(void); This gives me error that expected identifier.

4 REPLIES 4
TDK
Guru

it needs to be declared with extern "C" in your cpp file, not your header file.

https://isocpp.org/wiki/faq/mixing-c-and-cpp#call-cpp

If you feel a post has answered your question, please click "Accept as Solution".

Thank you for your reply. If it is declared as extern "C" in header file. Is there a way to use this function in .c file?

TDK
Guru

No, it needs C linkage to be called from C.

If you feel a post has answered your question, please click "Accept as Solution".

Typically you'd want to create an interface function in the .CPP file with the extern "C", and then you'd call *that* function from the .C file

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..