Ошибка компиляции при сборке CPP с Pybind11 для Python в Visual Studio 2017 - PullRequest
0 голосов
/ 29 декабря 2018

Этот запрос относительно учебника под названием Создание расширения C ++ для Python в Microsoft Docs.

Я выполнил все инструкции в соответствии с учебником.Я использую 64-битную Windows 10 и у меня установлено несколько версий Python, которые я обычно называю py -x.x-xx sample.py.Помимо этого, я установил Visual Studio 2017 с опцией разработки Python, которая включает в себя как 32-битный, так и 64-битный Python 3.6.

В учебном пособии показаны два способа: (1) CPythonрасширения и (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

Ответы [ 2 ]

0 голосов
/ 30 января 2019

Как я вижу, вы пытаетесь скомпилировать пример с 32-битным Python, и он не может скомпилировать pybind11 в рамках установки Python.

По умолчанию у меня установлен 64-битный Python отдельно с моим VS2017,Итак, я настроил пример для 64-битной системы следующим образом.

В superfastcode2 перейдите по адресу:

C / C ++ / General / Additional Включить каталоги - добавьте $(PythonHome)Include и $(ProjectDir)pybind11-master,Последний должен указать место, где установлен pybind11 (скачать его в виде ZIP-файла с GitHub и разархивировать в папку pybind11-master, он у меня внутри папки superfastcode2).Linker / General - добавьте $(PythonHome)libs

В суперскоростном коде перейдите по адресу:

C / C ++ / General / Дополнительные каталоги включения - добавьте

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\include

Linker / General - добавьте

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\libs

Добавьте этот обходной путь для Python.h в module.cpp:

//Workaround for - LNK1104: cannot open file 'python36_d.lib'

#define HAVE_RND  //This makes always use the Release version of Python, even in Debug mode

#ifdef _DEBUG
#define RESTORE_DEBUG
#undef _DEBUG
#endif

#include <Python.h>

#ifdef RESTORE_DEBUG
#define _DEBUG
#undef RESTORE_DEBUG
#endif

Исправьте путь к:

#include <include/pybind11/pybind11.h>

Тогда должно работать.;)

0 голосов
/ 08 января 2019

У меня была точно такая же проблема.После того, как все закончилось, я скачал репозиторий, связанный с руководством, на github: https://github.com/Microsoft/python-sample-vs-cpp-extension и построчно прошел через конфигурацию проекта.(Между прочим, их конфигурация использует переменную окружения PYTHONPATH вместо PYTHONHOME, как указано в руководстве.)

Для меня исправлена ​​проблема с изменением определений препроцессора на: _WINDLL;% (PreprocessorDefinitions), чтобы соответствоватьверсия репозитория.Похоже, виновником является определение Py_LIMITED_API, предложенное в руководстве: когда я добавляю это в список, проблема возвращается.

...