pybind / pybind/cuda_example

Using pybind not as add_subdirectory...

Open
#123 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
670
Forks
219
Avg merge
8d 2h
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the shown CMake configuration and main.cpp, then reproduce the Visual Studio 2022 configuration where find_package selects Python 3.10 instead of 3.7. Compare the Python paths and package discovery behavior between CLion and Visual Studio; done means the module builds against the requested Python 3.7 installation and imports successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp, python
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.