如何解决vcpkg和pybind11之间的冲突

问题描述 投票:0回答:1

系统:

Mac OSX 15.0
apple clang 16.0.0
cmake 3.30.1

在最新版本的 CMake 中,查找 python 的过程已发生变化。

假设我有以下目录设置

extern/vcpkg/  # cloned from github and bootstrapped
extern/pybind11/  # cloned from github
CMakeLists.txt
main.cpp

包含以下内容:

# CMakeLists.txt

# cmake_minimum_required(VERSION 3.26.0 FATAL_ERROR)
cmake_minimum_required(VERSION 3.27.0 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

# set (CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/extern/vcpkg/scripts/buildsystems/vcpkg.cmake)

set(PROJECT mytest)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
project(${PROJECT})
add_subdirectory(extern/pybind11)

pybind11_add_module(${PROJECT} main.cpp)
// main.cpp

#include <pybind11/pybind11.h>
#include <iostream>

void say_hello(){ std::cout << "Hello world!" << std::endl; }

PYBIND11_MODULE(mytest, m) { m.def("say_hello", &say_hello); }

然后我构建:

rm -rf build || true && cmake -H. -Bbuild && cmake --build build --target all

在此配置下,模块构建并运行得很好

如果我取消注释

CAMKE_TOOLCHAIN_FILE
行,则构建会失败并显示以下消息:

CMake Error at extern/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package):
  By not providing "FindPythonInterp.cmake" in CMAKE_MODULE_PATH this project
  has asked CMake to find a package configuration file provided by
  "PythonInterp", but CMake did not find one.

  Could not find a package configuration file provided by "PythonInterp"
  (requested version 3.8) with any of the following names:

    PythonInterpConfig.cmake
    pythoninterp-config.cmake

  Add the installation prefix of "PythonInterp" to CMAKE_PREFIX_PATH or set
  "PythonInterp_DIR" to a directory containing one of the above files.  If
  "PythonInterp" provides a separate development package or SDK, be sure it
  has been installed.
Call Stack (most recent call first):
  extern/pybind11/tools/FindPythonLibsNew.cmake:114 (find_package)
  extern/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package)
  extern/pybind11/tools/pybind11Tools.cmake:44 (find_package)
  extern/pybind11/tools/pybind11Common.cmake:200 (include)
  extern/pybind11/CMakeLists.txt:229 (include)

如果我随后恢复 cmake 所需的最低版本,则策略设置会发生更改,构建会再次成功,但会出现来自 pybind 的以下警告:

CMake Warning (dev) at extern/pybind11/tools/FindPythonLibsNew.cmake:101 (message):
  Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules
  are removed.  Run "cmake --help-policy CMP0148" for policy details.  Use
  the cmake_policy command to set the policy and suppress this warning, or
  preferably upgrade to using FindPython, either by calling it explicitly
  before pybind11, or by setting PYBIND11_FINDPYTHON ON before pybind11.
Call Stack (most recent call first):
  extern/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package)
  extern/pybind11/tools/pybind11Tools.cmake:44 (find_package)
  extern/pybind11/tools/pybind11Common.cmake:200 (include)
  extern/pybind11/CMakeLists.txt:229 (include)

我真的不知道 vcpkg 会影响这里的设置,所以如果有人知道,这将有助于理解。 此外,我试图了解是否可以采取任何措施来解决此问题,以便我可以使用最新的策略设置

c++ cmake pybind11 vcpkg
1个回答
0
投票

Pybind11 使用策略 CMP0148 玩肮脏游戏,请参见例如代码位于CMakeLists.txt:15。可能,vcpkg 出于其自身原因修改了该策略,并且该修改使 Pybind11 感到困惑(根据您的错误日志,Pybind11 尝试使用

FindPythonInterp.cmake
脚本,该脚本在 CMake 3.27 中标记为已删除)。

由于您对 CMake 版本的最低要求高于 3.12,其中添加了

FindPython.cmake
脚本,您可以安全地告诉 Pybind11 无条件使用该脚本而不是已弃用的脚本。这可以通过设置来实现

set(PYBIND11_FINDPYTHON ON)

在深入了解 Pybind11 代码之前。 (这是最后一条警告消息建议您的内容。)

© www.soinside.com 2019 - 2024. All rights reserved.