【问题标题】:Build Boost Extension with distutils and Microsoft Visual Studio in Anaconda在 Anaconda 中使用 distutils 和 Microsoft Visual Studio 构建 Boost 扩展
【发布时间】:2018-12-31 11:32:54
【问题描述】:

我尝试在我的 anaconda 安装(版本 5)(使用虚拟环境)中使用带有 distutils 的 boost 库构建扩展。代码是来自James Gregson.的MWE

我的setup.py

from distutils.core import setup, Extension
import sys, glob, os

# define the name of the extension to use
extension_name = 'ExtensionExample'
extension_version = '1.0'
libdir = r'C:\Users\schmmark\Anaconda3\envs\widy640\Library\lib'

# define the directories to search for include files
# to get this to work, you may need to include the path
# to your boost installation. Mine was in
# '/usr/local/include', hence the corresponding entry.
include_dirs = sys.path + [r'C:\Users\schmmark\Anaconda3\envs\widy640\Library\include', 'include',
                           r'C:\Users\schmmark\Anaconda3\envs\widy640\include']

# define the library directories to include any extra
# libraries that may be needed.  The boost::python
# library for me was located in '/usr/local/lib'
library_dirs = [r'C:\Users\schmmark\Anaconda3\envs\widy640\Library\lib']

# define the libraries to link with the boost python library
libraries = ['boost_python37-vc140-mt-x64-1_67']

# define the source files for the extension
source_files = ['src/boost_python_wrapper.cpp', 'src/functions_to_wrap.cpp', 'src/classes_to_wrap.cpp']

# define link arguments
# I change this for testing
# extra_compile_args = ['-DBOOST_ALL_NO_LIB']
# extra_compile_args = ['- -DBOOST_ALL_DYN_LINK']
extra_compile_args = []

# create the extension and add it to the python distribution
setup(name=extension_name, version=extension_version, ext_modules=[
    Extension(extension_name, source_files, include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries,
              extra_compile_args=extra_compile_args)])

使用此配置,对于命令python setup.py build 我收到错误

链接:致命错误 LNK1104:无法打开文件 'boost_pythonPY_MAJOR_VERSIONPY_MINOR_VERSION-vc140-mt-x64-1_67.lib'

即使文件boost_python37-vc140-mt-x64-1_67.lib 存在于文件夹C:\Users\schmmark\Anaconda3\envs\widy640\Library\lib 中。

当我设置extra_compile_args = ['-DBOOST_ALL_NO_LIB'] 时错误消失,但我不想手动导入所有标题。 msvc和boost有什么问题?

更新:

this answer的帮助下,我换上了boost/python/detail/config.hpp这一行

#define BOOST_LIB_NAME boost_python##PY_MAJOR_VERSION##PY_MINOR_VERSION

#define BOOST_LIB_NAME boost_python37

然后我收到链接错误

boost_python_wrapper.obj : error LNK2001: unresolved external symbol "bool __cdecl are_values_equal(int,int)" (?are_values_equal@@YA_NHH@Z)
boost_python_wrapper.obj : error LNK2001: unresolved external symbol "public: int __cdecl wrapped_class::get_value(void)const " (?get_value@wrapped_class@@QEBAHXZ)
boost_python_wrapper.obj : error LNK2001: unresolved external symbol "public: void __cdecl wrapped_class::set_value(int)" (?set_value@wrapped_class@@QEAAXH@Z)
boost_python_wrapper.obj : error LNK2001: unresolved external symbol "public: __cdecl wrapped_class::wrapped_class(void)" (??0wrapped_class@@QEAA@XZ)
boost_python_wrapper.obj : error LNK2001: unresolved external symbol "public: __cdecl wrapped_class::wrapped_class(int)" (??0wrapped_class@@QEAA@H@Z)
boost_python_wrapper.obj : error LNK2001: unresolved external symbol "char const * __cdecl get_string(void)" (?get_string@@YAPEBDXZ)
boost_python_wrapper.obj : error LNK2001: unresolved external symbol "int __cdecl num_arguments(bool,bool,bool,bool)" (?num_arguments@@YAH_N000@Z)
build\lib.win-amd64-3.7\ExtensionExample.cp37-win_amd64.pyd : fatal error LNK1120: 7 unresolved externals

【问题讨论】:

    标签: python c++ boost visual-studio-2015 boost-python


    【解决方案1】:

    我发现了这两个错误。

    1. LNK1104 错误

    这似乎与使用的 boost 版本有关。在撰写本文时,boost 库是 anaconda 发行版中的 1.67 版。使用项目主页上最新的 v1.69 二进制文件时,错误消失了。从项目主页使用v1.67时,错误仍然存​​在。

    1. LNK2001 错误

    我提到的来自James Gregson 的示例,cpp 文件仍然是空的。如果您编写实际代码编译是可能的,例如为functions_to_wrap.cpp

    // returns a random string
    const char *get_string()
    {
       return "hello, world";
    };
    
    // returns true if values are equal
    bool are_values_equal( int a, int b )
    {
       return 0;
    };
    
    // returns the number of supplied arguments to demonstrate
    // boost::python's default argument overloading features
    int num_arguments( bool arg0, bool arg1=false, bool arg2=false, bool arg3=false )
    {
       return 0;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多