我很难理解如何使用pybind11 conan包。我可以使用其他的,但是pybind11给了我困难的时间。
我的出发点如下:
conanfile.txt:
[requires]
pybind11/2.7.1
[generators]
cmake
main.cpp:
#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必需)
如果添加一行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
如果我需要pybind/2.4.3
而不是conanfile.txt
中的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没有帮助。但无论如何,我不想用旧的吡咯烷酮。
尝试测试包中的conanfile
conancentral食谱包含一个测试包(套餐),它在创建包时会自动测试。我们试一试吧!
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.
我只是绝望的?
也许吧但是!我可以:
构建示例
构建示例
建立conancentral pybind11食谱!当我将测试包解压缩到一个单独的文件夹时,也会失败。瓦特??
发布于 2021-11-08 14:23:28
我在过去(两三年前)使用过吡嗪,它在柯南身上没有问题。然而,我现在试图安装它,并面临类似的问题。这可能与conan向Conan2.0的发展有关(今天发布了一个阿尔法版本 ),并且该配方没有更新以考虑更改。
您可能考虑的另一种方法是使用不同的生成器使用conan安装pybind。更具体地说,CMakeDeps生成器。使用CMakeDeps生成器,conan将为您创建一个pybind11-config.cmake
文件,您只需在CMakeLists.txt
中使用find_package(pybind11 REQUIRED)
即可。也就是说,在您的CMakeLists.txt
文件中没有"conan特定的内容“。
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中使用它。
Conan与CMake的更深层次集成
除了CMakeToolchain生成器之外,还可以使用CMakeDeps生成器。它生成一个conan_toolchain.cmake
文件,通过--toolchain conan_toolchain.cmake
传递给cmake命令。如果使用它,就不需要将list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}")
行添加到CMakeLists.txt文件中。此外,您在conan中指定的设置(如构建类型和编译器)将影响cmake。也就是说,您不需要同时在conan和cmake中指定这些内容。对于cmake集成,这似乎是conan在开始2.0版本中的方向。
https://stackoverflow.com/questions/69781924
复制相似问题