User forums => General (but related to Code::Blocks) => Topic started by: FieldTheorist on September 20, 2017, 01:26:46 am

Title: Using the Boost.Python API within CodeBlocks?
Post by: FieldTheorist 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.)
Title: Re: Using the Boost.Python API within CodeBlocks?
Post by: stahta01 on September 20, 2017, 03:41:05 am
Please read the rules for this site,9996.0.html (,9996.0.html)
And, read what code::blocks is and is not in the Wiki (

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.
Title: Re: Using the Boost.Python API within CodeBlocks?
Post by: BlueHazzard 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?
Title: Re: Using the Boost.Python API within CodeBlocks?
Post by: BlueHazzard on September 20, 2017, 12:50:35 pm
After reading this:

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...
Title: Re: Using the Boost.Python API within CodeBlocks?
Post by: FieldTheorist 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
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:
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__
(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
import sys
sys.path.append('.') # Or, if the [YourModule].py/.so files are located in a different folder, set the destination here
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.