Using pybind not as add_subdirectory...

未关闭
#123 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
cmake, cpp, python
领域
build-system

调研方向

从所示的 CMake 配置和 main.cpp 开始,然后重现 Visual Studio 2022 配置,其中 find_package 选择 Python 3.10 而不是 3.7。比较 CLion 和 Visual Studio 之间的 Python 路径及包发现行为;当模块针对所请求的 Python 3.7 安装完成构建并成功导入时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

Hey

So I have a little complex library here and I need to be "in control" of what goes where/how...
Here is my RnD test that "builds" but fails to work/import in python... would any1 be able to look in to it ?

cmake_minimum_required(VERSION 3.25)
project(exampleHowToPybind)

#add_compile_options(/Zi /MDd /Od)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi /Od")
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG")

set(PY_BIND_ROOT "S:/01_Code//pybind11/include")
set(PY_VERSION 37)
set(PY_EXE "C:/Program Files/Python37")
set(Python3_ROOT_DIR "C:/Program Files/Python37")
set(Python_INCLUDE_DIRS "${Python3_ROOT_DIR}/include")
set(Python3_FIND_ABI "ON" "3" "7")
set(PYTHON_EXECUTABLE "${Python3_ROOT_DIR}/python.exe" CACHE FILEPATH "Path to the Python executable")


set(PY_BUILD_DEB)
if (${CMAKE_BUILD_TYPE} MATCHES Debug)
    message("WERE IN DEBUG MODE")
    set(PY_BUILD_DEB "_d")
endif ()
set(_PYTHON_EXECUTABLE "${PY_EXE}/python.exe" CACHE STRING "Path to the Python executable")
set(_PYTHON_INCLUDE_DIR "${PY_EXE}/include" CACHE STRING "Path to the Python include directory")
set(_PYTHON_LIBRARY "${PY_EXE}/libs/python${PY_VERSION}${PY_BUILD_DEB}.lib" CACHE STRING "Path to the Python library")


set(OLD_PATH ${CMAKE_PREFIX_PATH})
set(CMAKE_PREFIX_PATH ${PY_EXE})
#find_package(Python3 REQUIRED COMPONENTS Interpreter Development)# HINTS "${PYTHON_EXECUTABLE}")

#set(CMAKE_PREFIX_PATH ${PY_BIND_ROOT}/tools)
#find_package(pybind11)

set(CMAKE_PREFIX_PATH ${OLD_PATH})

message("  PYTHON_EXECUTABLE : ${_PYTHON_EXECUTABLE}")
message(" PYTHON_INCLUDE_DIR : ${_PYTHON_INCLUDE_DIR}")
message("     PYTHON_LIBRARY : ${_PYTHON_LIBRARY}")
message("   Python_LIBRARIES : ${Python_LIBRARIES}")
message("Python_INCLUDE_DIRS : ${Python_INCLUDE_DIRS}")

# include("${PY_BIND_ROOT}/CMakeLists.txt")

add_library(pybind11 INTERFACE)
target_include_directories(pybind11 INTERFACE ${PY_BIND_ROOT})


set(SRC main.cpp)

#add_executable(${PROJECT_NAME} ${SRC})
add_library(${PROJECT_NAME} MODULE ${SRC})

set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".pyd" )


target_link_libraries(${PROJECT_NAME} PRIVATE pybind11 ${PYTHON_LIBRARY} "${PY_EXE}/libs/python${PY_VERSION}.lib")
target_include_directories(${PROJECT_NAME} PRIVATE ${Python_INCLUDE_DIRS})

Now my current problem is that I can't use find_package. It works fine in Clion, but in visual studio 2022 it returns python 310 instead of 37 that I specified. No matter what I try he's ignoring my requests. In any case, I've decided to ignore that find package and link by hand....

Can any1 help out ?
@dean0x7d @henryiii

主要语言
C++
星标
671
派生
220
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

pybind/cuda_example 的其他 Issue

查看 pybind/cuda_example 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。