- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此查询与标题为 Create a C++ extension for Python 的教程有关在 Microsoft 文档中。
我已按照教程完成所有说明。我使用的是 Windows 10 64 位并安装了多个 python 版本,我通常将其称为 py -x.x-xx sample.py
。为此,我安装了带有 Python 开发选项的 Visual Studio 2017,其中包括 Python 3.6 32 位和 64 位。
教程中展示了两种方式:(1) CPython extensions和 (2) Pybind11 .他们两个都出现了多个错误。但是,我决定采用 Pybind11 方式,因此列出了与之相关的错误,因为它更简洁,并且只需对原始 C++ 代码进行很少的更改。
这是module.cpp
文件的一部分:
#include <pybind11/pybind11.h>
#include <Windows.h>
#include <cmath>
const double e = 2.7182818284590452353602874713527;
double sinh_impl(double x) {
return (1 - pow(e, (-2 * x))) / (2 * pow(e, -x));
}
double cosh_impl(double x) {
return (1 + pow(e, (-2 * x))) / (2 * pow(e, -x));
}
double tanh_impl(double x) {
return sinh_impl(x) / cosh_impl(x);
}
namespace py = pybind11;
PYBIND11_MODULE(superfastcode2, m) {
m.def("fast_tanh2", &tanh_impl, R"pbdoc(
Compute a hyperbolic tangent of a single argument expressed in radians.
)pbdoc");
#ifdef VERSION_INFO
m.attr("__version__") = VERSION_INFO;
#else
m.attr("__version__") = "dev";
#endif
}
当我尝试按照教程中的说明进行构建时,以下是日志中生成的错误:
module.cpp
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(49): error C2061: syntax error: identifier 'Py_buffer'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(49): error C2535: 'pybind11::buffer_info::buffer_info(void)': member function already defined or declared
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(26): note: see declaration of 'pybind11::buffer_info::buffer_info'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(87): error C2143: syntax error: missing ';' before '*'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(87): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(87): error C2238: unexpected token(s) preceding ';'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(50): error C2065: 'view': undeclared identifier
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(51): error C2065: 'view': undeclared identifier
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(50): error C2661: 'pybind11::buffer_info::buffer_info': no overloaded function takes 2 arguments
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(52): error C2039: 'view': is not a member of 'pybind11::buffer_info'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(17): note: see declaration of 'pybind11::buffer_info'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(52): error C2065: 'view': undeclared identifier
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(71): error C2065: 'view': undeclared identifier
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(71): error C2039: 'view': is not a member of 'pybind11::buffer_info'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(17): note: see declaration of 'pybind11::buffer_info'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(77): error C2065: 'view': undeclared identifier
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(77): error C3861: 'PyBuffer_Release': identifier not found
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\buffer_info.h(77): error C2541: 'delete': cannot delete objects that are not pointers
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(406): error C3861: 'PyInstanceMethod_Check': identifier not found
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(407): error C3861: 'PyInstanceMethod_GET_FUNCTION': identifier not found
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(410): error C3861: 'PyMethod_Check': identifier not found
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(411): error C3861: 'PyMethod_GET_FUNCTION': identifier not found
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(613): error C2065: 'PyListObject': undeclared identifier
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(613): error C2059: syntax error: ')'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(613): error C2612: trailing '(' illegal in base/member initializer list
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(678): error C2039: 'sequence_slow_readwrite': is not a member of 'pybind11::detail::iterator_policies'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(595): note: see declaration of 'pybind11::detail::iterator_policies'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(678): error C2065: 'sequence_slow_readwrite': undeclared identifier
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(678): error C2923: 'pybind11::detail::generic_iterator': 'sequence_slow_readwrite' is not a valid template type argument for parameter 'Policy'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(679): error C2039: 'dict_readonly': is not a member of 'pybind11::detail::iterator_policies'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(595): note: see declaration of 'pybind11::detail::iterator_policies'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(679): error C2065: 'dict_readonly': undeclared identifier
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(679): error C2923: 'pybind11::detail::generic_iterator': 'dict_readonly' is not a valid template type argument for parameter 'Policy'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(779): error C2027: use of undefined type '_typeobject'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\object.h(344): note: see declaration of '_typeobject'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(779): error C2065: '_PyObject_NextNotImplemented': undeclared identifier
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(832): error C2039: 'PyIterable_Check': is not a member of 'pybind11::detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(402): note: see declaration of 'pybind11::detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(832): error C3861: 'PyIterable_Check': identifier not found
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(839): error C2039: 'PyUnicode_Check_Permissive': is not a member of 'pybind11::detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(402): note: see declaration of 'pybind11::detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(839): error C3861: 'PyUnicode_Check_Permissive': identifier not found
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(934): error C2440: 'initializing': cannot convert from 'const pybind11::str' to 'pybind11::object'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(934): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(935): error C2027: use of undefined type 'pybind11::str'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(21): note: see declaration of 'pybind11::str'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(936): error C2027: use of undefined type 'pybind11::str'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(21): note: see declaration of 'pybind11::str'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(963): error C2039: 'PyNone_Check': is not a member of 'pybind11::detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(402): note: see declaration of 'pybind11::detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(963): error C3861: 'PyNone_Check': identifier not found
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1012): error C2039: 'enable_if_t': is not a member of 'pybind11::detail::detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(984): note: see declaration of 'pybind11::detail::detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1012): error C2947: expecting '>' to terminate template-parameter-list, found '<'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1012): warning C4346: 'value': dependent name is not a type
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1012): note: prefix with 'typename' to indicate a type
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1012): error C2061: syntax error: identifier 'value'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1013): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1029): error C2039: 'enable_if_t': is not a member of 'pybind11::detail::detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(984): note: see declaration of 'pybind11::detail::detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1029): error C2947: expecting '>' to terminate template-parameter-list, found '<'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1029): warning C4346: 'value': dependent name is not a type
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1029): note: prefix with 'typename' to indicate a type
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1029): error C2061: syntax error: identifier 'value'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1030): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1301): error C2244: 'pybind11::detail::object_api<Derived>::begin': unable to match function definition to an existing declaration
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1301): note: see declaration of 'pybind11::detail::object_api<Derived>::begin'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1301): note: definition
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1301): note: 'pybind11::detail::iterator pybind11::detail::object_api<Derived>::begin(void) const'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1301): note: existing declarations
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1301): note: 'pybind11::iterator pybind11::detail::object_api<Derived>::begin(void) const'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1302): error C2244: 'pybind11::detail::object_api<Derived>::end': unable to match function definition to an existing declaration
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1302): note: see declaration of 'pybind11::detail::object_api<Derived>::end'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1302): note: definition
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1302): note: 'pybind11::detail::iterator pybind11::detail::object_api<Derived>::end(void) const'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1302): note: existing declarations
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1302): note: 'pybind11::iterator pybind11::detail::object_api<Derived>::end(void) const'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1303): error C2888: 'pybind11::detail::item_accessor pybind11::detail::object_api<Derived>::operator [](pybind11::handle) const': symbol cannot be defined within namespace 'detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1306): error C2988: unrecognizable template declaration/definition
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1306): error C2059: syntax error: 'const'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1306): error C2143: syntax error: missing ';' before '{'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1306): error C2447: '{': missing function header (old-style formal list?)
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1309): error C2888: 'pybind11::detail::obj_attr_accessor pybind11::detail::object_api<Derived>::attr(pybind11::handle) const': symbol cannot be defined within namespace 'detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1312): error C2988: unrecognizable template declaration/definition
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1312): error C2059: syntax error: 'const'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1312): error C2143: syntax error: missing ';' before '{'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1312): error C2447: '{': missing function header (old-style formal list?)
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1315): error C2888: 'pybind11::detail::args_proxy pybind11::detail::object_api<Derived>::operator *(void) const': symbol cannot be defined within namespace 'detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1317): error C2027: use of undefined type 'pybind11::detail::args_proxy'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(25): note: see declaration of 'pybind11::detail::args_proxy'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1318): error C2888: 'bool pybind11::detail::object_api<Derived>::contains(T &&) const': symbol cannot be defined within namespace 'detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1323): error C2888: 'pybind11::str pybind11::detail::object_api<Derived>::str(void) const': symbol cannot be defined within namespace 'detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1323): error C2027: use of undefined type 'pybind11::str'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(21): note: see declaration of 'pybind11::str'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1326): error C2888: 'pybind11::detail::str_attr_accessor pybind11::detail::object_api<Derived>::doc(void) const': symbol cannot be defined within namespace 'detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\pytypes.h(1329): error C2888: 'pybind11::handle pybind11::detail::object_api<Derived>::get_type(void) const': symbol cannot be defined within namespace 'detail'
c:\program files (x86)\microsoft visual studio\shared\python36_86\include\pybind11\detail\typeid.h(22): fatal error C1903: unable to recover from previous error(s); stopping compilation
最佳答案
我遇到了完全相同的问题。由于失败,我在 github 上下载了与教程相关的存储库:https://github.com/Microsoft/python-sample-vs-cpp-extension并逐行完成项目配置。 (顺便说一句,他们的配置使用环境变量 PYTHONPATH 而不是教程中指示的 PYTHONHOME。)
解决我问题的方法是将预处理器定义更改为:_WINDLL;%(预处理器定义)以匹配存储库版本。罪魁祸首似乎是本教程建议的 Py_LIMITED_API 定义:当我将其添加到列表中时,问题又来了。
关于python - 在 Visual Studio 2017 中使用 Pybind11 for Python 构建 CPP 时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53972030/
首先,感谢大家努力解决我的这个疑问。我正在转换一个最小的 C++ 项目以在 Python 中使用。这种努力背后的真正原因是为了速度。 我遇到了 PyBind,对它的功能以及他们提供的文档数量感到非常惊
我有以下类结构(我实际实现的简化示例): /* TestClass.hpp */ #pragma once template class CurRecTemplate { protected:
我有以下设置(1 个基类、1 个派生类、1 个容器)。容器采用 shared_ptr作为输入。 #include namespace py = pybind11; struct Base { };
我刚刚安装了 pybinding,我正在尝试运行该库文档中提出的第一个示例。 import pybinding as pb import numpy as np import matplotlib.p
我正在使用 pybind 包装一些 C++ 函数,然后在 Python 中使用它。我需要一些结构,但我不知道如何在 Python 中访问它的属性。我的结构没有只有方法的属性,所以我认为绑定(bind)
我使用以下命令创建了一个 miniconda 环境: conda create -n build_a_python_cpp_module xtensor-python -c conda-forge 激
为了更好地理解如何使用 pybind 库将参数从 Python 传递到 C++ 函数,我想构建一个小的虚拟/演示代码,我可以在其中接收 C++ 端的 Python 列表,将其转换为浮点指针对象,然后打
我正在尝试编译 Pybind11 C++ 中的模块它调用了几个头文件(.h)在上面。由于我有很多头文件,我决定做一个 Makefile ,没有问题,除了创建目标共享对象文件(s.o) .我需要这个共享
pybind 的新手 - 阅读文档,但我不了解如何将其应用于二维数组。 我有两个数组存储 3d 坐标 shape = (10,3) a = np.zeros(shape=(10,3)) b = np.
TL;博士 将 pybind11 绑定(bind)添加到工作中的 C++ DLL 项目允许我在 Python 中导入和使用生成的 DLL,但会破坏我使用 boost/dll 机制在 C++ 代码中使用
在 Pybind 中,可以通知 Python 关于函数的参数名称: m.def("add", &add, "A function which adds two numbers", py::arg(
当我运行 pip install .在我有 setup.py 的目录中。我正在使用 pybind11 为我的 C++ 项目构建一个 python 模块。 (同样,在 Windows 10 上) 我收到
我正在尝试使用 PyBind 在 C++ 中嵌入一些 Python 代码。大多数文档都是关于使用 C++ 扩展 Python,但我对嵌入感兴趣: 关于 http://pybind11.readthed
我正在尝试使用 pybind11 实现 C++ 到 python,并使用 pybind11-multiprocessing-hangs 。不同之处在于我想在c++中使用python对象并继续从c++调
我是一名优秀的程序员,十分优秀!