Is it a good design to write the core main program in c++/shell script when the .so are in pure c? [closed]

StackOverflow https://stackoverflow.com/questions/15458321

  •  24-03-2022
  •  | 
  •  

Question

I would like to write a core main server handles request by user (in shell like enviorment) and load dynamic libraries executes ioctls and some debugfs. The core server is in c++ and the loadable libs are written in c. Is it a good design?


What about a shell script load the libraries and handles the request, is it possible at all?

Was it helpful?

Solution

C++ is designed with C compatibility in mind, so it is definitely OK to use libraries written in C from a program written in C++. The language even provides a syntax for using C headers with C++, so what you are trying to do is definitely possible.

In some cases, it may not be ideal from the readability standpoint, because C code may be using opaque handles to emulate objects: this is idiomatic to C, but not to C++. However, this issue can be easily adressed by writing a C++ wrapper around the C library, with very little overhead.

OTHER TIPS

The design in which a program or its part is written in C++ but interfaces to loadable modules in C is not unheard of. It is encountered wherever developers want to leverage the power of C++ (STL, exceptions) for the implementation, but want to retain C's simplicity and uniquity for the interface without dealing with, for example, subtle differences in how C++ compilers implement exceptions or RTTI. In Unix the additional reason is that there C++ has never taken off to the same level it did in Windows, so tooling support is still best for pure C.

An example of this would be third-party Python modules internally written in C++. Although implemented in C++, they communicate with Python using only C, as this is what the Python/C API expects. In some cases this communication can be rich - for example, C++ exceptions can be reported as Python exceptions and vice versa (Boost.Python does this automatically). The GTK-- toolkit allows writing GTK widgets in C++ and exporting their interface as regular GTK widgets.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top