Skip to content

Instantly share code, notes, and snippets.

@teeks99
Last active March 15, 2017 15:04
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teeks99/bb4921dffc8a74423c26 to your computer and use it in GitHub Desktop.
Save teeks99/bb4921dffc8a74423c26 to your computer and use it in GitHub Desktop.
Boost Python Quickstart Example - Getting it working

I wasn't able to get the basic quickstart example for Boost.Python working without some modifications, so I wanted to document it for others.

Here are the various issues I encountered with the default example:

  • The Bjam file (in the root of boost or in the quickstart, i'm not sure) defaults to threading=single, but something else is defaulting to threading=multi, so they are incompatible (shows up as saying the boost_python-vcXXX-mt-gd-1_XX.lib is not found when it just generated boost_python-vcXXX-gd-1_XX.lib). Manually specifying threading=multi for the bjam commands seems to fix things.
  • I wanted to get this going with boost 1.57.0, but there was a change to the boost::build directory structure in 1.56+ that hasn't been reflected in the quickstarts boost-build.jam file, so I reverted back to 1.55.0.
  • Visual Studio 2010 (msvc-10.0) has a bug that keeps it from building the quickstart\embedding.cpp example. I've done everything below with Visual Studio 2013 (msvc-12.0).
  • The rules to build the test_embed test don't seem to be creating a main(), leading to an unresolved external _mainCRTStartup symbol. (It looks like it is trying to make a test executable from the embedding.exe + instead of embedding.cpp)

Here's what I did to make things work.

  1. Install Python 2.7 from default install of python. (Maybe I'll try this again in the future with Python 3.) This doesn't include the builds with debug info (if you want that, you have to build from source). At first I thought this might have been causing me problems, but it appears that Boost.Python figures things out (as far as the correct linking). I've installed my version into C:\Python27-32\, this will allow me to install other versions along side (C:\Python27-64, C:\Python34-32, etc).

  2. Get the user-config.jam file correct! (This is by far the most important step!) I used:

    import toolset : using ;
    using msvc : 12.0 ;
    using python 
      : 2.7  # Version
      : C:\\Python27-32\\python.exe  # Interpreter
      : C:\\Python27-32\\include  # inc dir
      : C:\\Pyhton27-32\\libs  # link libs
      : # conditions
      ;
    
  3. Unzip boost (to f:\tmp\boost_1_55_0).

  4. Change to that directory (cd f:\tmp\boost_1_55_0)

  5. Bootstrap the bjam executable (bootstrap.bat)

  6. Change to the quickstart example directory (cd libs\python\example\quickstart)

  7. Run the "extending" test (this should build the correct version of Boost.Python as well) with the command: f:\tmp\boost_1_55_0\bjam.exe threading=multi toolset=msvc-12.0 --verbose-test test_ext. It should do a bunch of building, then run the test, one of the last lines should be something like **passed** bin\test_ext.test\msvc-12.0\release\threading-multi\test_ext.test

  8. Test the "embedding" application. Since the bjam file is broken, we have to do it by hand:

    1. Build the "embedding" application: f:\tmp\boost_1_55_0\bjam.exe threading=multi toolset=msvc-12.0 --verbose-test embedding
    2. Copy the needed DLL: copy ..\..\..\..\bin.v2\libs\python\build\msvc-12.0\debug\threading-multi\boost_python-vc120-mt-gd-1_55.dll .
    3. Run the app with the correct input: bin\msvc-12.0\debug\threading-multi\embedding.exe script.py
    4. You should see a bunch of python output (including a traceback, which is expected), and in the end 'No errors detected.'

If you want to do a 64-bit build as well, use this.

user-config.jam

    import toolset : using ;
    using msvc : 12.0 ;
    using python 
      : 2.7  # Version
      : C:\\Python27-32\\python.exe  # Interpreter
      : C:\\Python27-32\\include  # inc dir
      : C:\\Python27-32\\libs  # link libs
      : # conditions
      ;
    using python 
      : 2.7  # Version
      : C:\\Python27-32\\python.exe  # Interpreter
      : C:\\Python27-32\\include  # inc dir
      : C:\\Python27-32\\libs  # link libs
      : <address-model>64  # conditions
      ;    

command: f:\tmp\boost_1_55_0\bjam.exe threading=multi toolset=msvc-12.0 address-model=64 --verbose-test test_ext

Bonus: You can add a -jN to multi thread the build and make it go much faster. Just subsitute N with the number of processors you want to use.

@akochepasov
Copy link

Thanks a lot for sharing this. I lost a day in vain till find your helpful gist.

@sjtsilvester
Copy link

Works for python 2 but not for 3.4. Running the command to run the extending test does not display any errors, but it does not build the .exe file or attempt to run any tests.

EDIT: This happens with python 2 now. Presumably an issue with something I've done rather than the python version, but I don't know what it is.

@eciu
Copy link

eciu commented Aug 13, 2015

I cannot make it work. Im using boost 1.58 and python 3.4 . Right now after setting everything as in your solution I cannot make test_ext to be builded. I end up with following error:

Creating library bin\msvc-12.0\debug\threading-multi\extending.pdb and object
bin\msvc-12.0\debug\threading-multi\extending.exp
LINK : fatal error LNK1207: incompatible PDB format in 'C:\biist_1_58_0\libs\pyt
hon\example\quickstart\bin\msvc-12.0\debug\threading-multi\extending.pdb'; delet
e and rebuild

(not to mention than normally bjam cannot find properly build-systen.jam . Defualt path leads to not existing ..\tools\build\v2 folder (using tools\build\src instead).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment