配置Visual Studio以与Boost.Python和Python 3配合使用 [英] Configuring Visual Studio to work with Boost.Python and Python 3
问题描述
我在装有Windows 10 Pro的PC上安装了Microsoft Visual Studio Community 2013(版本12.0.31101.00更新4)和Python 3.6.1(v3.6.1:69c0db5,2017年3月21日).
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.
为了尝试使用Boost.Python的示例,我下载了boost 1.64.0并通过b2
使用选项--with-python --toolset=msvc --build-type=complete
构建库.结果,我得到了以下文件:
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
- 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
然后我在Visual Studio中创建了项目(类型:Win32/DLL),并采用了以下代码
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);
}
在C/C ++设置的项目属性中,我在Boost和Python的位置(以\ Python36 \ include结尾)中添加了其他包含目录".
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:
错误1错误LNK1104:无法打开文件'python36.lib'
Error 1 error LNK1104: cannot open file 'python36.lib'
因此,在链接器设置其他库目录"的项目属性中,我添加了相应的位置(以\ Python \ Python36 \ libs结尾).之后,我可以继续...进入下一个错误:
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:
错误1错误LNK1104:无法打开文件'boost_python-vc120-mt-gd-1_64.lib'
Error 1 error LNK1104: cannot open file 'boost_python-vc120-mt-gd-1_64.lib'
值得注意的是,我所拥有的文件名与VS2013所查找的文件名的区别只是单词python
之后的数字3
.
It is noteworthy that the difference in filenames I had and VS2013 looking for is just digit 3
after word python
.
在 google组进行了讨论,但没有有价值的提示.唯一有用的信息是库文件名*boost_python-*
对应于Python 2,而*boost_python3-*
对应于Python 3.
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.
我注意到将构建类型(解决方案配置)从调试"更改为发布"会导致部分库文件名中更改错误消息(现在没有-gd-
):
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):
错误1错误LNK1104:无法打开文件'boost_python-vc120-mt-1_64.lib'
Error 1 error LNK1104: cannot open file 'boost_python-vc120-mt-1_64.lib'
我想,VS2013知道boost库文件名约定,但可能不知道关于Python 2和Python 3的区别.
I suppose, VS2013 knows boost library file name convention, but probably does not know the difference about Python 2 and Python 3.
所以,我有3个问题:
- 是否有可能影响VS用于查找Boost.Python库的逻辑? (当然,也可以重命名lib-files,但是出于某些原因,我不喜欢这样)
- 链接器选项是否允许直接指定lib-file? (即,我可以写入
boost_python3-vc120-mt-1_64.lib
的完整路径,包括文件名,而不仅仅是其他库目录"部分中的文件夹名) - 项目属性中的哪个选项应使VS2013使用不同的LIB或DLL文件,例如是
libboost_python3-vc120-mt-1_64.lib
还是boost_python3-vc120-mt-1_64.dll
而不是boost_python-vc120-mt-1_64.lib
?
- 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)
- 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") - 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
orboost_python3-vc120-mt-1_64.dll
instead ofboost_python-vc120-mt-1_64.lib
?
推荐答案
在社区的帮助下,我找到了一些问题的答案.
With the community help I have found answers to couple of the questions.
- 是否有可能影响VS用于查找Boost.Python库的逻辑?
库的名称取决于在文件boost/python/detail/config.hpp
中定义为宏BOOST_LIB_NAME
的值.
我已尝试更改线路(boost 1.64.0中为108)
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
到
#define BOOST_LIB_NAME boost_python3
所需的库文件从boost_python-vc120-mt-1_64.lib
更改为boost_python3-vc120-mt-1_64.lib
.
And desirable library file changed from boost_python-vc120-mt-1_64.lib
to boost_python3-vc120-mt-1_64.lib
.
应该注意的是,可以在重新定义BOOST_LIB_NAME
的情况下创建并使用config.hpp
文件auto_link.hpp
中的值,而不用更改它们.
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
.
- 项目属性中的哪个选项应使VS2013使用不同的LIB或DLL文件,例如是
libboost_python3-vc120-mt-1_64.lib
还是boost_python3-vc120-mt-1_64.dll
而不是boost_python-vc120-mt-1_64.lib
?
- 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
orboost_python3-vc120-mt-1_64.dll
instead ofboost_python-vc120-mt-1_64.lib
?
这也受到定义的约束.
特别是,将代码添加到我的代码的开头(在#include <boost/python.hpp>
之前)
In particular, adding to the beginning of my code (before #include <boost/python.hpp>
) the line
#define BOOST_PYTHON_STATIC_LIB
强制MSVS搜索文件libboost_python3-vc120-mt-1_64.lib
(或libboost_python-vc120-mt-1_64.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
或
#define BOOST_ALL_DYN_LINK
可用于从dll导入代码.
can be used to import code from a dll.
这篇关于配置Visual Studio以与Boost.Python和Python 3配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!