【发布时间】:2021-12-15 07:10:20
【问题描述】:
我无法理解如何使用 pybind11 conan 包。我可以使用其他一些,但 pybind11 让我很难过。
我的出发点如下:
conanfile.txt:
[requires]
pybind11/2.7.1
[generators]
cmake
main.py:
#include <pybind11/pybind11.h>
int add(int i, int j) {return i + j;}
PYBIND11_MODULE(cobind, m) {m.def("add", &add);}
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(cobind11 VERSION 1.0 LANGUAGES CXX)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
pybind11_add_module(cobind main.cpp)
我试图完成这项工作的痛苦日志:
????起点
如果我尝试按上述方式构建项目,则会收到以下错误:
CMake Error at CMakeLists.txt:10 (pybind11_add_module):
Unknown CMake command "pybind11_add_module".
-- Configuring incomplete, errors occurred!
??????????添加 find_package(pybind11 REQUIRED)
如果我添加一行 find_package(pybind11 REQUIRED) 我会收到以下错误:
CMake Error at CMakeLists.txt:16 (find_package):
By not providing "Findpybind11.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "pybind11",
but CMake did not find one.
????????????添加 include([...]/pybind11Tools.cmake)
如果我添加一行 #include(${CONAN_PYBIND11_ROOT}/lib/cmake/pybind11/pybind11Tools.cmake) 我会收到以下错误:
CMake Error at /home/[...]/pybind11Tools.cmake:100 (set_property):
set_property could not find TARGET pybind11::pybind11. Perhaps it has not yet been created.
喜欢这个问题https://github.com/pybind/pybind11/issues/3388。也许它在新版本中已修复?
??????????????????升级到 2.8.1
新鲜的pybind11是2.8.1,升级吧
pybind11/2.8.1: Not found in local cache, looking in remotes...
pybind11/2.8.1: Trying with 'conancenter'...
ERROR: Unable to find 'pybind11/2.8.1' in remotes
好的。人们可以从字里行间看出它曾经可以工作,所以也许让我们降级?
??????????????????????降级到 2.4.3
如果我在conanfile.txt 中需要pybind/2.4.3 而不是pybind/2.7.1,我会得到
fatal error: Python.h: No such file or directory
112 | #include <Python.h>
| ^~~~~~~~~
喜欢这个问题https://github.com/pybind/pybind11/issues/1781。与该问题不同,安装 python*-dev 没有帮助。但无论如何,我不想使用旧的 pybind。
????????????????????????尝试测试包中的conanfile
conancentral 配方包含一个测试包 (https://github.com/conan-io/conan-center-index/tree/master/recipes/pybind11/all/test_package),在创建包时会自动对其进行测试。让我们试试吧!
CMake Error at CMakeLists.txt:13 (find_package):
By not providing "Findpybind11.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "pybind11",
but CMake did not find one.
我是不是没有希望了???
也许,但是!我可以:
????构建https://github.com/pybind/cmake_example
???构建https://github.com/pybind/python_example
???构建 conancentral pybind11 配方!当我将测试包提取到单独的文件夹时,同样的失败。什么?? ????
【问题讨论】:
-
尝试在包含
pybind11_add_module(cobind main.cpp)的行前添加find_package(pybind11 REQUIRED)。 -
不幸的是,它不适用于其他错误(我将粘贴在帖子中)。但是,与此同时,我在 pybind11 github.com/pybind/pybind11/pull/3420 中找到了这个 PR,因此可能会在下一个版本中开始工作。
-
在由柯南生成的“conanbuildinfo.cmake”文件中,您包含在您的 CMakeLists.txt 文件中,为不同的依赖项设置了许多变量。特别是,您会为每个依赖项以及该依赖项的正确库文件夹获得一个“CONAN_LIB_DIRS_
”变量。这意味着您可以使用 ${CONAN_LIB_DIRS_PYBIND11}/cmake/pybind11/pybind11Tools.cmake来获取该文件的完整路径。 -
您发布的 pybind 问题是针对多配置生成器的,但您使用的是 cmake 生成器。因此,它与您的问题无关。
-
conan 中可用的 pybind 最新版本是 2.7.1(请使用
conan search pybind11 -r=all进行检查)。您可以创建问题(或拉取请求)here 以添加新版本。