How to call a C++ function which is compiled with C++ compiler in C code?

Dani AI

Generated

A compact, practical follow‑up to the replies from and that fills in build, debug and design details for calling C++ code from C.

Compile/link notes (GCC/Clang)

# compile the C++ implementation with the C++ compiler
g++ -c cpp_impl.cpp -o cpp_impl.o

# compile the C caller with the C compiler
gcc -c main.c -o main.o

# link using the C++ driver so the C++ runtime is pulled in automatically
g++ main.o cpp_impl.o -o program

Linking with gcc instead of g++ is possible but then -lstdc++ (and possibly other flags) must be added manually. Link order and using the C++ linker are common causes of "undefined reference" errors.

Debugging exported symbols

  • Use nm -C or objdump -t on Unix to list object/DLL symbols; c++filt demangles names if needed.
  • On Windows, use dumpbin /EXPORTS to inspect DLL exports.
    These tools help verify whether the function is actually visible under the name the C linker expects.

Platform and runtime cautions

  • On Windows, exported symbols and calling conventions matter; exports are commonly controlled with a module definition file or export attributes, and the calling convention must match between caller and callee.
  • Ensure the same ABI (32/64 bit), calling convention, and compatible C runtime usage; crossing different CRTs can break heap/memory semantics.

API design and safety

  • Expose only C-friendly types across the boundary: plain integers, pointers to opaque handles, and C strings. Provide create/destroy and accessor functions so C never needs to know C++ internals.
  • Do not expose STL types or let exceptions unwind into C. Catch exceptions inside the C++ side and return error/status codes.
  • If memory is allocated on the C++ side, provide a matching free function on that side to avoid allocator/runtime mismatches.

For reference on language linkage and platform specifics see the C/C++ linkage documentation (cppreference) and the MSVC guidance on C language linkage (). This complements 's wrapper suggestion and addresses 's point about which functions can be used from plain C.

Recommended Answers

All 2 Replies

Please be more specific... is it a user defined function or a standard function like, say, strncpy()? Most of the standard functions of C are available in C++ but a few C++ specific functions won't be available to C and you cannot use them in C.
Please give more details.

How to call a C++ function which is compiled with C++ compiler in C code?

The function must either be compiled with C compatibility in place or wrapped in such a function, otherwise C won't be able to recognize it due to C++'s name mangling conventions:

extern "C" {
    void foo()
    {
        // Do C++ stuff
    }
}

Then you can link with the resulting object code from C and call the function successfully.

http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

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.