在 macos 上从 pybind11 文档构建 python_example 时出错

Posted

技术标签:

【中文标题】在 macos 上从 pybind11 文档构建 python_example 时出错【英文标题】:Errors building python_example from pybind11 docs on macos 【发布时间】:2019-08-12 06:15:36 【问题描述】:

按照 pybind11 文档中的说明进行编译时出现编译器错误,该示例构建位于 https://github.com/pybind/python_example

说明是 (1) 克隆 repo,以及 (2) pip3 install ./python_example。

我在 OS X (Mojave) 上使用 python 3.7.3 和

c++ --version
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

我对 python 包和 pybind11 非常陌生,所以我有点迷茫,希望能得到新手指导。看起来错误与 C++ 运算符 new 和 delete 的实现有关。 如果是这样,我相信我不是第一个遇到这个问题的人,并且会很感激一个解决方案的指针。

我按照 github repo 中给出的步骤进行操作:

Cloning into 'python_example'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 132 (delta 4), reused 11 (delta 4), pack-reused 119
Receiving objects: 100% (132/132), 31.03 KiB | 373.00 KiB/s, done.
Resolving deltas: 100% (62/62), done.
$ pip3 install ./python_example/

这是完整的输出。存储库中的某些内容是否配置错误(我没有进行配置)?这是python版本的东西吗?缺少编译器标志?如果我使用此设置作为模板分发代码,我需要做些什么来增加可移植性吗?我有点担心“hello world”示例让我处于堆栈溢出状态!提前感谢您的帮助。

Processing ./python_example
Requirement already satisfied: pybind11>=2.3 in /usr/local/lib/python3.7/site-packages (from python-example==0.0.1) (2.3.0)
Building wheels for collected packages: python-example
  Building wheel for python-example (setup.py) ... error
  Complete output from command /usr/local/opt/python/bin/python3.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/pip-req-build-9f8udd0w/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /private/var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/pip-wheel-fk0w8_oq --python-tag cp37:
  running bdist_wheel
  running build
  running build_ext
  creating var
  creating var/folders
  creating var/folders/7l
  creating var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn
  creating var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c /var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/tmpag19sot6.cpp -o var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/tmpag19sot6.o -std=c++17
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c /var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/tmpf327436e.cpp -o var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/tmpf327436e.o -fvisibility=hidden
  building 'python_example' extension
  creating build
  creating build/temp.macosx-10.14-x86_64-3.7
  creating build/temp.macosx-10.14-x86_64-3.7/src
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -I/usr/local/include/python3.7m -I/Users/stevescott/Library/Python/3.7/include/python3.7m -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c src/main.cpp -o build/temp.macosx-10.14-x86_64-3.7/src/main.o -stdlib=libc++ -mmacosx-version-min=10.7 -DVERSION_INFO="0.0.1" -std=c++17 -fvisibility=hidden
  In file included from src/main.cpp:1:
  In file included from /usr/local/include/python3.7m/pybind11/pybind11.h:49:
  In file included from /usr/local/include/python3.7m/pybind11/attr.h:13:
  /usr/local/include/python3.7m/pybind11/cast.h:579:34: error: aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on macOS 10.14 or newer
                          vptr = ::operator new(type->type_size,
                                   ^
  /usr/local/include/python3.7m/pybind11/cast.h:579:34: note: if you supply your own aligned allocation functions, use -faligned-allocation to silence this diagnostic
  In file included from src/main.cpp:1:
  /usr/local/include/python3.7m/pybind11/pybind11.h:1009:9: error: no matching function for call to 'operator delete'
          ::operator delete(p, s, std::align_val_t(a));
          ^~~~~~~~~~~~~~~~~
  note: candidate function not viable: requires 2 arguments, but 3 were provided
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:188:36: note: candidate function not viable: requires 2 arguments, but 3 were provided
  _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
                                     ^
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:190:74: note: candidate function not viable: requires 2 arguments, but 3 were provided
  _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
                                                                           ^
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:221:40: note: candidate function not viable: requires 2 arguments, but 3 were provided
  inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT 
                                         ^
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:187:36: note: candidate function not viable: requires single argument '__p', but 3 arguments were provided
  _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
                                     ^
  In file included from src/main.cpp:1:
  /usr/local/include/python3.7m/pybind11/pybind11.h:1011:9: error: call to unavailable function 'operator delete': introduced in macOS 10.12
          ::operator delete(p, s);
          ^~~~~~~~~~~~~~~~~
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:190:74: note: candidate function has been explicitly made unavailable
  _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
                                                                           ^
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:221:40: note: candidate function not viable: no known conversion from 'pybind11::size_t' (aka 'unsigned long') to 'void *' for 2nd argument; take the address of the argument with &
  inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT 
                                         ^
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:188:36: note: candidate function not viable: no known conversion from 'pybind11::size_t' (aka 'unsigned long') to 'const std::nothrow_t' for 2nd argument
  _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
                                     ^
  note: candidate function not viable: no known conversion from 'pybind11::size_t' (aka 'unsigned long') to 'std::align_val_t' for 2nd argument
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:187:36: note: candidate function not viable: requires single argument '__p', but 2 arguments were provided
  _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
                                     ^
  3 errors generated.
  error: command 'clang' failed with exit status 1

  ----------------------------------------
  Failed building wheel for python-example
  Running setup.py clean for python-example
Failed to build python-example
Installing collected packages: python-example
  Running setup.py install for python-example ... error
    Complete output from command /usr/local/opt/python/bin/python3.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/pip-req-build-9f8udd0w/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/pip-record-yvb3nloo/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_ext
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c /var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/tmpkd6a1ugc.cpp -o var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/tmpkd6a1ugc.o -std=c++17
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c /var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/tmpywf858_h.cpp -o var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/tmpywf858_h.o -fvisibility=hidden
    building 'python_example' extension
    creating build
    creating build/temp.macosx-10.14-x86_64-3.7
    creating build/temp.macosx-10.14-x86_64-3.7/src
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -I/usr/local/include/python3.7m -I/Users/stevescott/Library/Python/3.7/include/python3.7m -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c src/main.cpp -o build/temp.macosx-10.14-x86_64-3.7/src/main.o -stdlib=libc++ -mmacosx-version-min=10.7 -DVERSION_INFO="0.0.1" -std=c++17 -fvisibility=hidden
    In file included from src/main.cpp:1:
    In file included from /usr/local/include/python3.7m/pybind11/pybind11.h:49:
    In file included from /usr/local/include/python3.7m/pybind11/attr.h:13:
    /usr/local/include/python3.7m/pybind11/cast.h:579:34: error: aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on macOS 10.14 or newer
                            vptr = ::operator new(type->type_size,
                                     ^
    /usr/local/include/python3.7m/pybind11/cast.h:579:34: note: if you supply your own aligned allocation functions, use -faligned-allocation to silence this diagnostic
    In file included from src/main.cpp:1:
    /usr/local/include/python3.7m/pybind11/pybind11.h:1009:9: error: no matching function for call to 'operator delete'
            ::operator delete(p, s, std::align_val_t(a));
            ^~~~~~~~~~~~~~~~~
    note: candidate function not viable: requires 2 arguments, but 3 were provided
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:188:36: note: candidate function not viable: requires 2 arguments, but 3 were provided
    _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
                                       ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:190:74: note: candidate function not viable: requires 2 arguments, but 3 were provided
    _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
                                                                             ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:221:40: note: candidate function not viable: requires 2 arguments, but 3 were provided
    inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT 
                                           ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:187:36: note: candidate function not viable: requires single argument '__p', but 3 arguments were provided
    _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
                                       ^
    In file included from src/main.cpp:1:
    /usr/local/include/python3.7m/pybind11/pybind11.h:1011:9: error: call to unavailable function 'operator delete': introduced in macOS 10.12
            ::operator delete(p, s);
            ^~~~~~~~~~~~~~~~~
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:190:74: note: candidate function has been explicitly made unavailable
    _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
                                                                             ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:221:40: note: candidate function not viable: no known conversion from 'pybind11::size_t' (aka 'unsigned long') to 'void *' for 2nd argument; take the address of the argument with &
    inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT 
                                           ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:188:36: note: candidate function not viable: no known conversion from 'pybind11::size_t' (aka 'unsigned long') to 'const std::nothrow_t' for 2nd argument
    _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
                                       ^
    note: candidate function not viable: no known conversion from 'pybind11::size_t' (aka 'unsigned long') to 'std::align_val_t' for 2nd argument
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/new:187:36: note: candidate function not viable: requires single argument '__p', but 2 arguments were provided
    _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
                                       ^
    3 errors generated.
    error: command 'clang' failed with exit status 1

    ----------------------------------------
Command "/usr/local/opt/python/bin/python3.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/pip-req-build-9f8udd0w/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/pip-record-yvb3nloo/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/7l/5r4qgkms4vb3xm6ktxdvh1l80000gn/T/pip-req-build-9f8udd0w/

【问题讨论】:

【参考方案1】:

我在 Mac OS 上的 pybind11 项目遇到了同样的问题。

我无法找出根本原因,但找到了解决方法:我通过从列表 here 中删除 -std=c++17 来禁用 C++17 cpp 标志。

因此,我没有收到您提到的相同错误,而是将它们作为警告。这不是一个合适的解决方案,我真的很想看到一个真正的解释,但至少我已经编译了我的源代码。

更新: 正如eudoxos 在他的评论中指出的那样,他们在 pybind11 的 github 页面上打开了一个与此相关的issue,修复应该在下一个版本中。

【讨论】:

在此处跟踪问题:github.com/pybind/pybind11/issues/1604(包括解决方法)

以上是关于在 macos 上从 pybind11 文档构建 python_example 时出错的主要内容,如果未能解决你的问题,请参考以下文章

使用 pybind11 构建 cpp

如何为 pybind11 正确构建活页夹

返回多个 py::array 而不在 pybind11 中复制

在 Windows 上从源代码构建 NSIS(VS2012)

如何在pybind11中将共享指针列表传递给c++对象

如何将浮点数传递给期望 int 的 pybind11 函数