3
votes

I'm trying to follow this tutorial on embedding Python into a C++ application, and am hitting a roadblock right from the beginning. Let me take you through it...

  1. I extract the boost library to C:\codelibraries\c++\boost_1_55_0
  2. I creat a new project in VC+++ and create a 'test1.cpp' file in it with the following code:

    #include <boost/python.hpp>
    
    int main(int, char**) {
     Py_Initialize();
    
     Py_Finalize();
     return 0;
    }
    
  3. I then place the following directories in my VC++ Directories > Include Directories:
    • C:\codelibraries\c++\boost_1_55_0
    • C:\codelibraries\c++\boost_1_55_0\boost\python
    • C:\Python27\include
  4. In my VC++ Directories > Library Directories:
    • C:\codelibraries\c++\boost_1_55_0
    • C:\codelibraries\c++\boost_1_55_0\libs
    • C:\Python27\libs
  5. And in my VC++ Directories > Source Directories:
    • C:\Python27\include
    • C:\codelibraries\c++\boost_1_55_0

After adding these and running it, I am given error LNK1104: cannot open file 'boost_python-vc100-mt-gd-1_55.lib'.

Okay, turns out that I need to actually install the boost library. Fine. I do so. It creates the new folder '..\boost_1_55_0\stage\lib.' Inside it, there is the library file called libboost_python-vc100-mt-gd-1_55.lib and another one that's very similar but excludes the 'gd' portion.

Under 'Linker' > 'General' > 'Additional Library Directories', I added C:\codelibraries\c++\boost_1_55_0\stage\lib. I run it again. still it gives the same linker error LNK1104: cannot open file 'boost_python-vc100-mt-gd-1_55.lib'. I go to the 'stage\lib' folder and see that there is no boost_python-vc100-mt-gd-1_55.lib, only *lib*boost_python-vc100-mt-gd-1_55.lib.

I rename libboost_python-vc100-mt-gd-1_55.lib to boost_python-vc100-mt-gd-1_55.lib and rerun the build.

It succeeds, but gives me other linker errors:

error LNK1120: 3 unresolved externals
error LNK2001: unresolved external symbol __imp___Py_NoneStruct
error LNK2019: unresolved external symbol __imp__Py_Finalize referenced in function _main
error LNK2019: unresolved external symbol __imp__Py_Initialize referenced in function _main

And now I'm stuck and have come here. Are the few lines of code I have written incorrect? Does it have anything to do with my using a 64-bit machine? Are my includes incorrect? Please help if you can. Any information is greatly appreciated, thank you.

2
You need to install Python and add its header and library locations to the VC search paths. But there's something strange going on with your setup. The lib prefix gets added auto added to the library name, so you shouldn't have to do any renaming. Try opening <boost/python.hpp> and trace backwards to where <boost/config/auto_link.hpp> is being included, and see if you can figure out why auto linking is getting the name wrong.Praetorian
You also need to link to the python VM's import library.sehe
@Praetorian I actually had the 'C:\Python\include' headers folder in the 'Include Directories' as well. I'm not sure how to trace backwards and figure out the whole autolinking issue, but I will say I also tried to run bjam.exe on the Python example folder deep within the '..\libs\python\build\..' and Boost.Build is also looking for the python lib without the 'lib' prefix. Everything in my 'stage\lib' folder has the 'lib' prefix already attached...akappel

2 Answers

4
votes

I'm returning to this question as I do not want to leave it unanswered. It turns out the issue was attempting to use a 64-bit implementation of Python with a 32-bit version of the boost libraries. After switching to 32-bit Python, things got a lot easier. If you do not have to use 64-bit Python, I would recommend against it.

1
votes

In my case, there are only .lib files begin with libboost* rather than boost* in stage/lib. So I have to go to download the binaries from http://boost.teeks99.com/ which have all libboost*.lib, boost*.lib and boost*.dll versions and add the boost*.lib and boost*.dll to stage\lib library in order for it to work.