【问题标题】:Cmake: using conan pybind11 packageCmake:使用柯南 pybind11 包
【发布时间】: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 以添加新版本。

标签: cmake pybind11 conan


【解决方案1】:

我过去(两三年前)使用过 pybind,它对柯南没有任何问题。但是,我现在尝试安装它并遇到了类似的问题。这可能与柯南向柯南 2.0 的演变(今天发布了 alpha version)有关,并且配方没有根据帐户更改进行更新。

您可能会考虑另一种方法,即使用 不同的生成器 安装带有 conan 的 pybind。更具体地说,CMakeDeps 生成器。使用 CMakeDeps 生成器,柯南将为您创建一个pybind11-config.cmake 文件,您只需在CMakeLists.txt 中使用find_package(pybind11 REQUIRED)。也就是说,您的 CMakeLists.txt 文件中没有“柯南特定的东西”。

conanfile.txt

[requires]
pybind11/2.7.1

[generators]
CMakeDeps

CMakeLists.txt

cmake_minimum_required(VERSION 3.21)
project(cobind11 VERSION 1.0 LANGUAGES CXX)

# Tell cmake to also search in the buld folder for the "<library>-config.cmake"
# files
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}")
find_package(pybind11 REQUIRED)

pybind11_add_module(cobind main.cpp)

main.cpp

#include <pybind11/pybind11.h>

int add(int i, int j) { return i + j; }

PYBIND11_MODULE(cobind, m) { m.def("add", &add); }

有了这个,我能够构建库并在 Python 中使用它。


柯南与 CMake 的深度集成

除了 CMakeDeps 生成器之外,您还可以使用 CMakeToolchain 生成器。它会生成一个conan_toolchain.cmake 文件,您可以使用--toolchain conan_toolchain.cmake 将其传递给cmake 命令。如果您使用它,则无需将list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}") 行添加到您的 CMakeLists.txt 文件中。此外,您在 conan 中指定的设置,例如构建类型和编译器,将影响 cmake。也就是说,你不需要在 conan 和 cmake 中都指定这些东西。这似乎是柯南在即将到来的 2.0 版本中关于 cmake 集成的方向。

【讨论】:

  • 太好了,非常感谢! ?
  • 这不起作用,我用这种方法得到完全相同的错误。
猜你喜欢
  • 2023-03-11
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多