Configuring Visual Studio to work with Boost.Pytho

2019-02-26 17:40发布

问题:

I had Microsoft Visual Studio Community 2013 (Version 12.0.31101.00 Update 4) and Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017) on my PC with Windows 10 Pro.

In order to try examples with Boost.Python I downloaded boost 1.64.0 and build libraries by b2 with options --with-python --toolset=msvc --build-type=complete. As a result I have the following files:

  • boost_python3-vc120-mt-1_64.dll
  • boost_python3-vc120-mt-1_64.lib
  • boost_python3-vc120-mt-gd-1_64.dll
  • boost_python3-vc120-mt-gd-1_64.lib
  • libboost_python3-vc120-mt-1_64.lib
  • libboost_python3-vc120-mt-gd-1_64.lib
  • libboost_python3-vc120-mt-s-1_64.lib
  • libboost_python3-vc120-mt-sgd-1_64.lib
  • libboost_python3-vc120-s-1_64.lib
  • libboost_python3-vc120-sgd-1_64.lib

Then I created project (type: Win32 / DLL) in Visual Studio with the following code taken here:

char const* greet()
{
    return "hello, world";
}

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello)
{
    using namespace boost::python;
    def("greet", greet);
}

In project properties for C/C++ settings I added "Additional Include Directories" to locations of Boost and Python (ends with \Python36\include).

During the first attempt to build the project an error appears:

Error 1 error LNK1104: cannot open file 'python36.lib'

So in project properties for Linker settings "Additional Library Directories" I added corresponding location (ends with \Python\Python36\libs). After that I could move on ... to the next error:

Error 1 error LNK1104: cannot open file 'boost_python-vc120-mt-gd-1_64.lib'

It is noteworthy that the difference in filenames I had and VS2013 looking for is just digit 3 after word python.

Similar questions at stackoverflow and in google groups are discussed but without valuable tips. The only useful information is that library file names *boost_python-* corresponds to Python 2 and *boost_python3-* to Python 3.

I noticed that changing the build type (Solution Configuration) from Debug to Release leads to change the error message in part of library file name (there is no -gd- now):

Error 1 error LNK1104: cannot open file 'boost_python-vc120-mt-1_64.lib'

I suppose, VS2013 knows boost library file name convention, but probably does not know the difference about Python 2 and Python 3.

So, I have 3 questions:

  1. Is it possible to influence the logic used by VS to look for Boost.Python library? (Of course lib-files renaming is also an option, but I do not like this for some reason)
  2. Do the linker options allow specifying lib-file directly? (i.e. I can write whole path to the boost_python3-vc120-mt-1_64.lib including file name, not just folder name in section "Additional Library Directories")
  3. What option in the project properties should make VS2013 to use different LIB or DLL files, e.g. libboost_python3-vc120-mt-1_64.lib or boost_python3-vc120-mt-1_64.dll instead of boost_python-vc120-mt-1_64.lib?

回答1:

With the community help I have found answers to couple of the questions.

  1. Is it possible to influence the logic used by VS to look for Boost.Python library?

Name of library depends on value defined as macro BOOST_LIB_NAME in file boost/python/detail/config.hpp. I have tried to change line (108 in boost 1.64.0)

#define BOOST_LIB_NAME boost_python

to

#define BOOST_LIB_NAME boost_python3

And desirable library file changed from boost_python-vc120-mt-1_64.lib to boost_python3-vc120-mt-1_64.lib.

It should be noted, that instead of changing values in config.hpp file auto_link.hpp can be created and used with redefinition of BOOST_LIB_NAME.

  1. What option in the project properties should make VS2013 to use different LIB or DLL files, e.g. libboost_python3-vc120-mt-1_64.lib or boost_python3-vc120-mt-1_64.dll instead of boost_python-vc120-mt-1_64.lib?

That is also regulated by defines.

In particular, adding to the beginning of my code (before #include <boost/python.hpp>) the line

#define BOOST_PYTHON_STATIC_LIB

forces MSVS to search file libboost_python3-vc120-mt-1_64.lib (or libboost_python-vc120-mt-1_64.lib), i.e. static lib. And vice versa line

#define BOOST_PYTHON_DYNAMIC_LIB

or

#define BOOST_ALL_DYN_LINK

can be used to import code from a dll.