Код C++ выглядит следующим образом:
Код: Выделить всё
#include
int add(int i, int j) {
return i + j;
}
namespace py = pybind11;
PYBIND11_MODULE(cmake_module, m) {
m.def("add", &add, R"pbdoc(
Add two numbers
Some other explanation about the add function.
)pbdoc");
};
Код: Выделить всё
cmake_minimum_required(VERSION 3.30.2)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -shared")
set(CMAKE_CXX_FLAGS_DEBUG "-Werror -Og")
# Specify the path to pybind11 if it's not installed globally
set(PYBIND11_DIR "C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\pybind11")
# Specify the path to the Python interpreter
set(PYBIND11_FINDPYTHON "C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python312\\python.exe")
# Find pybind11
find_package(pybind11 REQUIRED PATHS ${PYBIND11_DIR})
# Find Python components
find_package(Python COMPONENTS Interpreter Development REQUIRED)
# Include directories for Python headers
include_directories(${Python_INCLUDE_DIRS})
# Create the module
set(MODULE_NAME cmake_example)
pybind11_add_module(${MODULE_NAME} src/test.cpp)
Код: Выделить всё
ImportError: DLL load failed while importing cmake_example: The specified module could not be found.Мои попытки решить проблему:
- Предоставил путь к моему интерпретатору Python в CMake.
- Попробовал os.add_dll_directory("path/to/build/dir")
- Попробовал os.add_dll_directory("path/to/build/dir")
- Попробовал os.add_dll_directory("path/to/build/dir")
- li>
Скомпилированный файл .pyd перемещен в каталог «C: Users\User\AppData\Local\Programs\Python\Python312\DLLs».
Подробнее здесь: https://stackoverflow.com/questions/790 ... th-pybind1