配置 Visual Studio 以使用 Boost.Python 和 Python 3

Configuring Visual Studio to work with Boost.Python and Python 3

我的电脑上安装了 Microsoft Visual Studio Community 2013(版本 12.0.31101.00 更新 4)和 Python 3.6.1(v3.6.1:69c0db5,2017 年 3 月 21 日)Windows 10 临.

为了尝试使用 Boost.Python 的示例,我下载了 boost 1.64.0 并通过 b2 使用选项 --with-python --toolset=msvc --build-type=complete 构建了库。结果我有以下文件:

然后我在 Visual Studio 中创建了项目(类型:Win32/DLL),代码如下 here:

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

#include <boost/python.hpp>

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

在 C/C++ 设置的项目属性中,我将 "Additional Include Directories" 添加到 Boost 和 Python 的位置(以 \Python36\include 结尾)。

在第一次尝试构建项目时出现错误:

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

所以在链接器设置的项目属性中 "Additional Library Directories" 我添加了相应的位置(以 \Python\Python36\libs 结尾)。之后我可以继续......下一个错误:

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

值得注意的是,我的文件名和 VS2013 寻找的文件名的区别只是单词 python.

之后的数字 3

讨论了 Whosebug and in google groups 中的类似问题,但没有有价值的提示。唯一有用的信息是库文件名 *boost_python-* 对应于 Python 2 和 *boost_python3-* 对应于 Python 3.

我注意到将构建类型(解决方案配置)从调试更改为发布会导致更改部分库文件名中的错误消息(现在没有 -gd-):

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

我想,VS2013 知道 boost 库文件命名约定,但可能不知道 Python 2 和 Python 3.

的区别

所以,我有 3 个问题:

  1. 是否可以影响 VS 用于查找 Boost.Python 库的逻辑? (当然 lib-files 重命名也是一个选项,但出于某种原因我不喜欢这个)
  2. 链接器选项是否允许直接指定 lib 文件? (即我可以写 boost_python3-vc120-mt-1_64.lib 的整个路径,包括文件名,而不仅仅是 "Additional Library Directories" 部分中的文件夹名称)
  3. 项目属性中的哪个选项应该使 VS2013 使用不同的 LIB 或 DLL 文件,例如libboost_python3-vc120-mt-1_64.libboost_python3-vc120-mt-1_64.dll 而不是 boost_python-vc120-mt-1_64.lib?

在社区的帮助下,我找到了几个问题的答案。

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

库的名称取决于在文件 boost/python/detail/config.hpp 中定义为宏 BOOST_LIB_NAME 的值。 我试过换线(108 in boost 1.64.0)

#define BOOST_LIB_NAME boost_python

#define BOOST_LIB_NAME boost_python3

并且所需的库文件从 boost_python-vc120-mt-1_64.lib 更改为 boost_python3-vc120-mt-1_64.lib

应该注意的是,可以创建文件 auto_link.hpp 而不是更改 config.hpp 中的值,并与 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?

这也是由定义规定的。

特别是,在我的代码开头(#include <boost/python.hpp> 之前)添加行

#define BOOST_PYTHON_STATIC_LIB

强制 MSVS 搜索文件 libboost_python3-vc120-mt-1_64.lib(或 libboost_python-vc120-mt-1_64.lib),即静态库。 反之亦然

#define BOOST_PYTHON_DYNAMIC_LIB

#define BOOST_ALL_DYN_LINK

可用于从 dll 导入代码。