Chapter 5. Playing Nice with Others
The count of programming languages approaches infinity, and a huge chunk of them have a C interface. This short chapter offers some general notes about the process and demonstrates in detail the interface with one language, Python.
Every language has its own customs for packaging and distribution, which means that after you write the bridge code in C and the host language, you get to face the task of getting the packaging system to compile and link everything. This gives me a chance to present more advanced tricks for Autotools, such as conditionally processing a subdirectory and adding install hooks.
The Process
I can’t give you details about how to write the bridge code for every language that calls C code (herein the host language), but the same problems must be surmounted in every case:
On the C side, writing functions to be easy to call from other languages.
Writing the wrapper function that calls the C function in the host language.
Handling C-side data structures. Can they be passed back and forth?
Linking to the C library. That is, once everything is compiled, we have to make sure that at runtime, the system knows where to find the library.
Writing to Be Read by Nonnatives
The host language has no access to your source code, and there will be constraints in calling C code from the host.
Macros are read by the preprocessor, so that the final shared library has no trace of them. In Chapter 10, I discuss all sorts of ways for you to use macros to ...
Get 21st Century C now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.