Example of how to create a plain C wrapper that calls C++ code. This is useful when working in a C-only environment and willing to use C++ code from a separate library. This repo provides an example of how to build the C++ code into a shared library and of how to write a C-only wrapper that calls and uses the C++ code.
-
cpp_class[.h|.cpp]
define a simple example C++ class with member functions that we want to call from C-only code. -
wrapper[.h|.cpp]
define a C-only interface that provides plain old functions that call the C++ code incpp_class
. -
Both
cpp_class
andwrapper
get compiled (using g++) into a shared library namedlibcpplib_wrapper.so
byMakefile
. -
example_main.c
is a C-only example program that is linked withlibcpplib_wrapper.so
and that can call the C++ code incpp_class
using the API provided bywrapper
. -
Running
make
usingMakefile
will generate the shared librarylibcpplib_wrapper.so
and the binaryexample_main
.
Build in a UNIX environment by calling:
$ make
You should then see the following files in your directory:
$ ls
cpp_class.cpp cpp_class.h cpp_class.o example_main example_main.c libcpplib_wrapper.so Makefile README.md wrapper.cpp wrapper.h wrapper.o
Update your LD_LIBRARY_PATH
environment variable before running example_main
:
$ export LD_LIBRARY_PATH=/home/user/.../cpplib_wrapper:$LD_LIBRARY_PATH
And now you can run the example program:
$ ./example_main
Successfully using C++ object from C, pointer: 0x1cbc010
42