Cmake:使用 conan pybind11 包

psa*_*rka 6 cmake conan pybind11

我无法理解如何使用 pybind11 conan 包。我可以使用其他一些,但 pybind11 给我带来了困难。

我的出发点是这样的:

柯南文件.txt:

[requires]
pybind11/2.7.1

[generators]
cmake
Run Code Online (Sandbox Code Playgroud)

主要.cpp:

[requires]
pybind11/2.7.1

[generators]
cmake
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

我试图让这项工作成功的痛苦日志:

初始点

如果我尝试按照上面的方式构建项目,我会收到以下错误:

CMake Error at CMakeLists.txt:10 (pybind11_add_module):
  Unknown CMake command "pybind11_add_module".

-- Configuring incomplete, errors occurred!
Run Code Online (Sandbox Code Playgroud)

添加find_package(需要pybind11)

如果我添加一行,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.
Run Code Online (Sandbox Code Playgroud)

添加 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.
Run Code Online (Sandbox Code Playgroud)

就像这个问题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
Run Code Online (Sandbox Code Playgroud)

好的。人们可以从字里行间看出,它曾经有效,所以也许我们可以降级?

降级到2.4.3

如果我需要pybind/2.4.3而不是pybind/2.7.1conanfile.txt我得到

fatal error: Python.h: No such file or directory
  112 | #include <Python.h>
      |          ^~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

就像这个问题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.
Run Code Online (Sandbox Code Playgroud)

我只是绝望吗???

也许吧,但是!我可以:

构建https://github.com/pybind/cmake_example
构建https://github.com/pybind/python_example
构建 conancentral pybind11 配方!当我将测试包提取到单独的文件夹时,同样会失败。啥啊??

柯南在折磨我

dar*_*amo 2

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

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

柯南文件.txt

[requires]
pybind11/2.7.1

[generators]
CMakeDeps
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

主程序

[requires]
pybind11/2.7.1

[generators]
CMakeDeps
Run Code Online (Sandbox Code Playgroud)

这样我就能够构建该库并在 Python 中使用它。


Conan 与 CMake 更深入的集成

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