The purpose of this post is to explain what you need to do to use use Boost from your C language code. The following are the steps required to use Boost from C:
- Decide which part of Boost that you require in your C application.
- Wrap that part of Boost as "convenience" C library.
- Link your C application code to the "convenience" library.
DISCLAIMERThe sample code basically wraps Boost SPSC (single producer single consumer) lockfree queue into a convenience C library and the sample application links to the convenience library.
-----------
- The code assumes that the platform in which it runs has a working pthread implementation.
- The code is not production quality code. Use it at your own risk.
Anyway, the most important part of the code that you need to understand is the part that "returns" a C++ class as an "opaque" C structure. Probably, it's a quite alien concept. But, I assure you that this alien concept is the core of C<-->C++ interoperability. Below is the relevant code snippets:
-->
//---- START spsc_interface.hpp file ----------------- #ifndef SPSC_WRAPPER_H #include "spsc_wrapper.h" #endif //SPSC_WRAPPER_H class spsc_interface { public: explicit spsc_interface(); explicit spsc_interface(const spsc_interface &); ~spsc_interface() {}; //... }; //---- END spsc_interface.hpp file ----------------- //---- START spsc_wrapper.h file ----------------- #ifdef __cplusplus extern "C" { #endif //... typedef struct spsc_interface spsc_interface; spsc_interface *create_spsc_interface(); //---- END spsc_wrapper.h file ----------------- //---- START spsc_wrapper.cc file ----------------- #include "spsc_interface.hpp" #include "spsc_wrapper.h" #ifdef __cplusplus extern "C" { #endif spsc_interface * create_spsc_interface() { return new spsc_interface(); } ... #ifdef __cplusplus } #endif //---- END spsc_wrapper.cc file -----------------
As you see above, the wrapper function create_spsc_interface() returns an opaque object of type struct spsc_interface. In fact, this opaque object is a C++ class in disguise as you can see in the spsc_wrapper.cc snippet above. If you ask: How's that possible? Well, the answer lies in the linking process. Let's see the relevant snippets from src/Makefile.am:
wrapper_test_SOURCES = wrapper_test.c ### Below is just a trick to force using C++ linker nodist_EXTRA_wrapper_test_SOURCES = dummy.cc
As you can see, we're not using a C linker to link the entire project, but we use a C++ linker (by tricking autotools ;) . A C++ linker "understand" the idiom used in spsc_wrapper.cc above.
Post a Comment
No comments:
Post a Comment