This is the extension of the problem I faced while messing with boost::asio library. So, finally I attempted to run different async loops in muti-threads and have spectacularly failed. Let me share some experiences of mine wrt. The following is the code using boost libraries , dont worry if you dont understand it much. So the easiest way to interface c++ with python is to create the "c" wrapper for the same and then creating a shared .so file for it. Python has an awesome (not so awesome actually ) library called cytpes where u can call .so files in ur python program.
So, compiling this program seems straight forward assuming boost is already been setup.
$ g++ -c -fPIC scale.cpp -o scale.o -L/usr/lib -lboost_system -lboost_thread
$ g++ -shared -Wl,-soname,libscale.so -o libscale.so scale.o
Soo ideally this should work right ? I mean why not you have created your object file with necessary boost links and then create a shared .so of the same. Lets create a simple python wrapper for the same and call the run module.
If u run the same using an interpreter , voila .. as soon as you load the library you will see the following error. Damn! its not able to identify the boost_thread symbols ... But how is this possible ?
>>> lib = cdll.LoadLibrary('./libscale.so')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/ctypes/__init__.py", line 431, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python2.6/ctypes/__init__.py", line 353, in __init__
self._handle = _dlopen(self._name, mode)
OSError: ./libscale.so: undefined symbol: _ZTIN5boost6detail16thread_data_baseE
rahulram ~/programs/cpp $ ldd ./libscale.so
linux-gate.so.1 => (0xb789c000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb775f000)
libm.so.6 => /lib/libm.so.6 (0xb7723000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7705000)
libc.so.6 => /lib/libc.so.6 (0xb75a7000)
/lib/ld-linux.so.2 (0xb789d000)
If u see the linked libraries , libscale.so doest not include the boost libraries. Why on the world will gcc compiler not warn me of that ? I have no frekin idea .. Finally I had to recreate the .so files mentionnig boost_threads and systems
rahulram~/programs/cpp $ g++ -shared -Wl,-soname,libscale.so -o libscale.so scale.o -lboost_system -lboost_thread
rahulram ~/programs/cpp $ ldd libscale.so
linux-gate.so.1 => (0xb783e000)
libboost_system.so.1.42.0 => /usr/lib/libboost_system.so.1.42.0 (0xb77e3000)
libboost_thread.so.1.42.0 => /usr/lib/libboost_thread.so.1.42.0 (0xb77cf000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb76e7000)
libm.so.6 => /lib/libm.so.6 (0xb76ab000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb768e000)
libc.so.6 => /lib/libc.so.6 (0xb7530000)
librt.so.1 => /lib/librt.so.1 (0xb7527000)
libpthread.so.0 => /lib/libpthread.so.0 (0xb750d000)
/lib/ld-linux.so.2 (0xb783f000)
ohh la la .. It seems to have linked the desired ilbraries . Now you can re-run the same python program, it should work..
PS: I learnt this the hard way :(
PS: I learnt this the hard way :(