简体   繁体   English

有人可以解释 pybind11 安装吗?

[英]Can someone explain the pybind11 install?

The pybind11 documentation is generally good, but one area in which it is not is explaining the install process and the process of getting and running examples using cmake. pybind11 文档总体上很好,但其中一个方面是解释安装过程以及使用 cmake 获取和运行示例的过程。

I've managed to figure out how to get and build examples.我已经设法弄清楚如何获取和构建示例。 But it leads to more questions.但这会导致更多的问题。 Almost all the examples count on downloading the pybind11 repo into the examples folder and including the root folder of the repo in a cmake run (the root folder contains a cmakelists.txt file).几乎所有示例都依赖于将 pybind11 存储库下载到示例文件夹中,并在 cmake 运行中包含存储库的根文件夹(根文件夹包含一个 cmakelists.txt 文件)。 The contents of that repo have a lot in common with the content which is added to a python environment when you install pybind11 using pip or conda.当您使用 pip 或 conda 安装 pybind11 时,该 repo 的内容与添加到 python 环境的内容有很多共同之处。 But the folder organization is completely different.但是文件夹组织完全不同。

So I'm curious:所以我很好奇:

  • Why the difference?为什么有区别?
  • Is there a way to use the content in the environment install in such a way that you don't also have to download the repo in order to build examples using cmake?有没有一种方法可以使用环境安装中的内容,这样您就不必下载 repo 来使用 cmake 构建示例?
  • Failing that, what is the best way to put the pybind11 repo in a common place so it doesn't have to be copied all over the place in order to build examples, or in order to provide the important added cmake functionality for one's own code?如果做不到这一点,将 pybind11 存储库放在一个公共位置的最佳方法是什么,这样就不必为了构建示例而到处复制它,或者为了为自己的代码提供重要的附加 cmake 功能?

I'm really uncomfortable in general not understanding the "how this works" aspect of such things, so this will really help me.总的来说,我真的很不舒服,不了解这些事情的“这是如何工作的”方面,所以这真的对我有帮助。

Not sure which examples you mention, but to install pybind11 in your system and use it in different projects, just follow the standard procedure for installing CMake based packages不确定您提到了哪些示例,但是要在您的系统中安装 pybind11 并在不同的项目中使用它,只需按照安装基于 CMake 的软件包的标准程序即可

mkdir build
cd build
cmake ../  # optionally you can specify -DPYBIND11_PYTHON_VERSION=<your python version>
make
sudo make install

Then in one of your other project's CMakeLists.txt you can use it eg like this:然后在您的其他项目之一的CMakeLists.txt中,您可以使用它,例如:

find_package(pybind11 CONFIG REQUIRED)
message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}")
add_library(<name of your lib> MODULE <your sources>)
target_link_libraries(mylib pybind11::module)

For more CMake commands consult pybind11Config.cmake .有关更多 CMake 命令,请参阅pybind11Config.cmake

Then if you don't want to install it in your system, you can just embed pybind11 repo in your project tree with add_subdirectory instead of find_package .然后,如果您不想在系统中安装它,您可以使用add_subdirectory而不是find_packagepybind11 repo 嵌入到项目树中。 All the offered features will be the same.所有提供的功能都是一样的。 The package is well designed and it detects whether it is used as master project or not and either defined INSTALL targets or not. package 设计精良,它可以检测它是否用作主项目以及是否定义了安装目标。

So I guess that your last 2 questions are answered?所以我猜你的最后两个问题得到了回答?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM