在Anaconda中使用distutils和Microsoft Visual Studio构建Boost扩展

时间:2018-12-31 11:32:54

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

我尝试在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,我收到错误消息

  

LINK:致命错误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

1 个答案:

答案 0 :(得分:0)

我发现了两个错误。

  1. LNK1104错误

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

  1. LNK2001错误

James Gregson中提到的示例cpp文件仍然为空。如果您编写实际的代码编译是可能的,例如为<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#1c1c1c"> <RelativeLayout android:id="@+id/toolbarRel" android:layout_width="match_parent" android:layout_height="@dimen/toolbar_height" android:layout_alignParentTop="true" android:layout_gravity="center_vertical" android:background="@color/color_toolbar"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_gravity="center_horizontal"> <TextView android:id="@+id/text_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/login" android:textColor="@color/white" android:textSize="@dimen/heading_font_size" android:textStyle="bold" /> </RelativeLayout> </RelativeLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/toolbarRel" android:background="#1c1c1c" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginTop="5dp" android:id="@+id/logindetails" android:gravity="center_vertical|center_horizontal" android:orientation="vertical" android:paddingStart="@dimen/activity_horizontal_margin" android:paddingEnd="@dimen/activity_horizontal_margin"> <EditText android:id="@+id/et_email" style="@style/common_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/textfield_outer_box" android:drawableStart="@drawable/img_email_icon" android:drawableLeft="@drawable/img_email_icon" android:drawablePadding="10dp" android:ems="10" android:hint="Email Id" android:inputType="textEmailAddress" android:paddingStart="10dp" android:paddingEnd="10dp" /> <EditText android:id="@+id/et_pwd" style="@style/common_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/homogeneous_margin" android:background="@drawable/textfield_outer_box" android:drawableStart="@drawable/img_password_icon" android:drawableLeft="@drawable/img_password_icon" android:drawablePadding="10dp" android:ems="10" android:hint="Password" android:inputType="textPassword" android:paddingStart="10dp" android:paddingEnd="10dp" /> <Button android:id="@+id/btn_login" style="@style/style_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/heterogenous_margin" android:background="@drawable/login_button" android:paddingBottom="@dimen/button_topbottom_padding" android:text="@string/get_started" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/homogeneous_margin" android:layout_below="@id/logindetails"> <TextView android:id="@+id/txt_forgot_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="13dp" android:gravity="center_vertical" android:text="Forgot Password?" android:textColor="@android:color/white" android:textSize="18sp" /> <TextView android:id="@+id/text_create_account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginTop="@dimen/homogeneous_margin" android:gravity="center" android:text="Create Account" android:textColor="@android:color/white" android:textSize="18sp" /> </RelativeLayout> </LinearLayout> </ScrollView> </RelativeLayout>

functions_to_wrap.cpp