Author Topic: Using the Boost.Python API within CodeBlocks?  (Read 250 times)

Offline FieldTheorist

  • Single posting newcomer
  • *
  • Posts: 2
Using the Boost.Python API within CodeBlocks?
« on: September 20, 2017, 01:26:46 am »
Has anyone here tried to use Boost.Python within the Codeblocks IDE?  If so, please tell me how you did it, I would be eternally grateful.

Is there any way of either:  (A.) using CodeBlocks directly to create a Python extension module (when linking to the Boost.Python), or (B.) a straightforward way (with helpful documentation) of converting SO files created by CodeBlocks into Python extension modules?  Again, I would really like to thank anyone can help point me in the right direction.  (C.) Anything you have done that has worked.  I would greatly appreciate your input!




(For those who don't know, Boost.Python is a C++ Boost library turning low-level C++ code into an extension-module for Python scripts.  I've spent all day on it, and while I can link the Boost.Python libraries within CodeBlocks and generate an SO file, I cannot find a single piece of information on how to convert an SO file into a Python extension library.  Most solutions either require you to use Bjam --which has essentially non-existent documentation and it wants to create the SO file from source itself-- or else Makefiles/Distutils with the exact same problems.)

Offline stahta01

  • Lives here!
  • ****
  • Posts: 5943
    • My Best Post
Re: Using the Boost.Python API within CodeBlocks?
« Reply #1 on: September 20, 2017, 03:41:05 am »
Please read the rules for this site http://forums.codeblocks.org/index.php/topic,9996.0.html
And, read what code::blocks is and is not in the Wiki http://wiki.codeblocks.org/index.php/FAQ#General

I can not say that your question is or is not on topic; but, I do think it would be more on topic on a different website.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Testing, compiling CB Trunk against wxWidgets 3.0.2.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline BlueHazzard

  • Lives here!
  • ****
  • Posts: 1520
Re: Using the Boost.Python API within CodeBlocks?
« Reply #2 on: September 20, 2017, 12:37:34 pm »
I never did something like this. And i don't have the time do learn it.
But you can describe the steps that are needed or post a example makefile and we can try to help you implementing it in codeblocks. I suppose there are some pre compiler and post compiler steps?

Offline BlueHazzard

  • Lives here!
  • ****
  • Posts: 1520
Re: Using the Boost.Python API within CodeBlocks?
« Reply #3 on: September 20, 2017, 12:50:35 pm »
After reading this:
https://stackoverflow.com/questions/3871724/using-boost-python-with-make-instead-of-bjam

you should simply be able to use this boost library by using the library normally? i am not that familiar with python, but i tought you will need some compiled python files for using the library. It seems that I am wrong...

Offline FieldTheorist

  • Single posting newcomer
  • *
  • Posts: 2
Re: Using the Boost.Python API within CodeBlocks?
« Reply #4 on: September 21, 2017, 02:41:12 am »
Right.  So I have found a work around.  I have no idea how robust this procedure is (given the simplicity, I am guessing it may have a harder time with more difficult modules, but I'll continue testing), but if this is what you want, go ahead and give this a try.  I'm being overly pedagogical here, because this is kind of frustrating:

1.) Create the .so shared library in CodeBlocks (click here to learn about the Boost.Python API).  This just involves installing the Boost.Python library, linking against it (and the Python API) in your compiler, and including the folder which contain your Python API.  I can't speak to compilations that aren't based on g++ compilers and for GNU/Linux OS's, but the toolchain should look something like
Quote
g++-5 -Wall -fexceptions -std=c++14 -fPIC -g -pedantic -I/usr/include/python3.5m -c example_source.cpp -o obj/Debug/example_source.o
g++-5 -shared  obj/Debug/example_source.o  -o bin/Debug/[NAME OF MODULE].so -lboost_python-py35 -lpython3.5m 
(Important stuff in bold:  I'm using Python 3.5, thus I am including the python3.5m library (python3.5 is also okay, 3.5m just handles memory size better) alongside the boost_python-py35 library (look for the SO file name in "/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu" or wherever your Python library is installed, lose the "lib" add "-l" and drop the ".so", and that's how you link), and remember that you often need -fPIC on CodeBlocks to make shared libraries.)

2.) In that folder, keeping the same convention as Boost.Python's module naming in your C++ source files, create a [module name].py file, and copy/paste:
Quote
def __bootstrap__():
   global __bootstrap__, __loader__, __file__
   import sys, pkg_resources, imp
   __file__ = pkg_resources.resource_filename(__name__,'[INSERT MY SO FILE NAME HERE].so')
   __loader__ = None; del __bootstrap__, __loader__
   imp.load_dynamic(__name__,__file__)
__bootstrap__()
(Don't ask me, I'm not an expert in pkg-resources or imp, but it seems that pkg-resources and imp can load the SO shared library's made for Python.)

3.) Now, in your Python file that uses this module, you may include the extension module via
Quote
import sys
sys.path.append('.') # Or, if the [YourModule].py/.so files are located in a different folder, set the destination here
import [INSERT YOUR MODULE NAME]
and then you can freely use your home brew C++ extension Python module, remembering obviously to use the correct Python interpreter (in this example, Python 3.5) and the correct names for the wrappers for your functions/classes.  Now all you have to do is learn how to use the Boost.Python library.


PS:  It would be really helpful if someone wrote a CodeBlocks script/wizard modification for classes/functions that automatically wrote a header/source/source IDL file (i.e. the special Boost.Python IDL code).  I think CodeBlocks has a scripting language called "Squirrel", I may look into doing this if this pkg_resources works consistently when I include classes and make a more sophisticated C++ extension-module for Python.
« Last Edit: September 21, 2017, 09:22:51 pm by FieldTheorist »