Skip to main content
Iswarya
Associate III
June 7, 2020
Question

Call C++ function from C file

  • June 7, 2020
  • 2 replies
  • 2424 views

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.

    This topic has been closed for replies.

    2 replies

    TDK
    Super User
    June 7, 2020

    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""."
    Iswarya
    IswaryaAuthor
    Associate III
    June 7, 2020

    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?

    Tesla DeLorean
    Guru
    June 7, 2020

    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 VenmoUp vote any posts that you find helpful, it shows what's working..
    TDK
    Super User
    June 7, 2020

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

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