How to include C++ code in C program

Last updated on March 2, 2023 by Dan Nanni

Question: I have C++ library that I want to use in my C program. How can I include C++ code in C program in general?

You can include C++ code in a C program, but you need to take some precautions to ensure that the C++ code is compiled correctly by the C compiler.

First of all, C++ is a superset of C, which means that any valid C code is also valid C++ code. However, C++ adds several features that are not available in C. One prominent C++ feature is object-oriented programming, where you define classes, instantiate class objects, and call methods on them. In C, you cannot create and use C++ class objects directly because C does not support object-oriented programming features like classes, objects, and inheritance. However, you can still use C++ code in your C program by creating a C++ wrapper for your C++ class.

Here's an example that shows how to use a C++ class in your C program.

In this example, I have created a C++ class MyClass with a method myMethod(). Suppose this is part of C++ library that you want to use in your C program.

// MyClass.h
class MyClass {
public:
    void myMethod();
};
// MyClass.cpp
#include "MyClass.h"
void MyClass::myMethod() {
    // Implementation of myMethod
}

Then what you can do is to write a C++ wrapper for this class definition as follows.

// CWrapper.h
#ifdef __cplusplus
extern "C" {
#endif
    typedef void* MyClassPtr;
    MyClassPtr createMyClass();
    void deleteMyClass(MyClassPtr ptr);
    void callMyMethod(MyClassPtr ptr);
#ifdef __cplusplus
}
#endif
// CWrapper.cpp
#include "MyClass.h"
#include "CWrapper.h"
MyClassPtr createMyClass() {
    return new MyClass();
}
void deleteMyClass(MyClassPtr ptr) {
    delete static_cast(ptr);
}
void callMyMethod(MyClassPtr ptr) {
    static_cast(ptr)->myMethod();
}

In the above code snippet, I have created a C++ wrapper for MyClass class in the CWrapper.cpp file. The wrapper defines three functions: createMyClass(), deleteMyClass(), and callMyMethod(), which can be used by a C program to create and delete a MyClass object, and call the myMethod() method on the object.

The "extern C" keyword tells the compiler that the function should have C language linkage. C++ uses a name mangling technique to encode additional information about a function or variable's type, which can make it difficult to call C++ functions from other languages or link C++ code with code written in other languages like C. Using "extern C" allows C++ functions to have a C-compatible interface, making them easier to call from C and other languages.

When you declare a function or variable with "extern C", the compiler will use C language linkage for that function or variable. This means that the function or variable's name will not be modified by the name mangling process, and its entry point in the compiled object code will have a predictable name that can be used by other languages.

How to Use C++ Wrapper in C Program

To use this C++ wrapper in your C program, you just need to include the CWrapper.h header file and link your program with the compiled CWrapper.cpp file. Here's an example of how you can use this wrapper in your C program:

#include "CWrapper.h"

int main() {
    MyClassPtr ptr = createMyClass();
    callMyMethod(ptr);
    deleteMyClass(ptr);
    return 0;
}

In this example, I have created a MyClass object using the createMyClass() function, called its myMethod() method using the callMyMethod() function, and deleted the object using the deleteMyClass() function.

The MyClassPtr type is defined as a "void*" pointer in the CWrapper.h header file to ensure that the C program does not access the internal members of the MyClass object directly. Instead, the C program can only use the wrapper functions to interact with the object.

Hope this helps.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2023 ‒ AboutWrite for Us ‒ Feed ‒ Powered by DigitalOcean