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

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)
{
    helloWorld();
    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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.