Call C++ function from C file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-06-07 8:59 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-06-07 9:33 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-06-07 9:36 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-06-07 9:41 AM
No, it needs C linkage to be called from C.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-06-07 11:01 AM
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
Up vote any posts that you find helpful, it shows what's working..
