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

Dani AI

Generated

is right: give the functions C linkage and call a C-friendly API from your C code. Expand that idea by putting a single header with C guards in front of all C-callable declarations, and write thin C-linkage wrappers for any C++-only constructs (classes, exceptions, STL types).

Example header (myapi.h) showing the pattern and an opaque handle type:

#ifdef __cplusplus
extern "C" {
#endif

typedef void* MyObjectHandle; /* opaque to C */

MyObjectHandle create_myobject(void);
void destroy_myobject(MyObjectHandle h);
int myobject_do_work(MyObjectHandle h, int param);

#ifdef __cplusplus
}
#endif

A C++ implementation for that header should provide the wrappers (note the extern "C" linkage block) and translate between the opaque handle and actual C++ objects. Keep wrappers responsible for new/delete and catch exceptions so they do not unwind into C code.

// cpp-wrapper.cpp
#include "myapi.h"
#include "MyClass.h"

extern "C" {

MyObjectHandle create_myobject(void) {
    return reinterpret_cast<MyObjectHandle>(new MyClass());
}

void destroy_myobject(MyObjectHandle h) {
    delete reinterpret_cast<MyClass*>(h);
}

int myobject_do_work(MyObjectHandle h, int p) {
    try {
        return reinterpret_cast<MyClass*>(h)->doWork(p);
    } catch (...) {
        return -1; /* map exceptions to error codes */
    }
}

} // extern "C"

Build and link carefully: compile the C++ code with your C++ compiler, compile the C file with the C compiler, then link with the C++ linker (or add the C++ runtime). Example using GNU tools:

g++ -c cpp-wrapper.cpp
gcc -c main.c
g++ main.o cpp-wrapper.o -o app   # g++ links libstdc++ for you

Practical cautions: do not expose C++ standard library types or overloaded/defaulted functions in the C API; ensure matching calling conventions (Windows DLLs often need __declspec(dllexport) and explicit calling convention macros); never let C++ exceptions cross into C; and prefer the same compiler/runtime for both sides to avoid allocator/ABI problems.

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.