I need to know how to work this out, say for example i have a method is C++ that is helloworld(), and i need to call it from a C program.

// in my C++ program
void helloWorld(){
       // some code;
       cout<<"hello world";
      some code.....

This is my C program, how do i call the hello world function?

int main( void ) {

// how do i call helloWorld() of C++ here ?

return 0;

i went through this tutorial and came across the above problem

6 Years
Discussion Span
Last Post by Narue

First write your C++ code with C linkage and a compatible interface:

#include <iostream>

extern "C" void helloWorld()
    std::cout<<"hello world\n";

Compile that into an object file. Now switch over to C:

#include <stdio.h>

extern void helloWorld(void);

int main(void)
    return 0;

Compile that and link in the C++ object file. Done. This is bare bones, of course. For any real C++ library you'd want to write a header instead of write the declaration manually in every C file.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.