I have a question about good practices for writing a C++ library that includes wrappers for other languages, such as Python and Matlab. This may be a simple question or a duplicate, but I haven't found a good resource or another answer which helps explain how to do this.
For background, I am working on a C++ library project that has C wrappers included specifically for compatibility with other languages. The library is a scientific computing library written in C++, and I have already written the C wrappers for the functions and classes to be used as part of a shared library.
My question is how to incorporate the C wrappers into the modules for other languages, such as wrapper libraries for Python and Matlab. I'm not asking for specifics on how to implement the code for these other languages, because that is another question entirely, and I already have a basic understanding of how to write the code that can be built and loaded for each language as its own library. My question is mainly about including the C wrappers into these other builds.
For example, I have the following directory structure, where each folder contains code relevant to a different language/module.
top
├─ matlab
│ ├─ CMakeLists.txt
│ ├─ matlab_wrapper.hpp
│ └─ matlab_wrapper.cc
├─ python
│ ├─ CMakeLists.txt
│ ├─ python_wrapper.hpp
│ └─ python_wrapper.cc
├─ src
│ ├─ CMakeLists.txt
│ ├─ c_wrapper.hpp
│ ├─ c_wrapper.cc
│ └─ other code...
└─ CMakeLists.txt
I understand that each folder should be self-sufficient, and buildable on its own, but I am having trouble figuring out how to distribute the code so that it includes the C wrappers. Basically, I have the C implementation in the c_wrapper
files and I would like to use this in the matlab and python libraries.
Normally, I would include the other source files during the build step and be done with it (this may just be the answer), but everything in the src
folder is built and distributed as its own shared library. It seems redundant to either rewrite the C wrappers in the matlab_wrapper
and python_wrapper
files (basically creating 3 versions of the same function) or to include the c_wrapper
files in the build step of the other libraries because they are already included in the main C++ library.
I'm not an expert in packaging shared libraries, so what is the best way to go about this? Should I just include the c_wrapper
source files as part of the build step, rewrite the C++ wrappers in each module, or is there another way to include the code in c_wrapper
in the wrapper libraries? Ideally, each module will share the same (or very similar) API, so it would be great if there were a way to include this code without rewriting the same function every time a new C wrapper is added. Any help or advice is greatly appreciated.