Я использую Windows
Когда я попробовал в Python, чтобы вызвать функцию C ++ is_five < /code>, все пошло плохо.>>> import is_five
>>> help(is_five)
Help on package is_five:
NAME
is_five
PACKAGE CONTENTS
FILE
(built-in)
>>> dir(is_five)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
>>> is_five.is_five
Traceback (most recent call last):
File "", line 1, in
AttributeError: module 'is_five' has no attribute 'is_five'
< /code>
Кажется, что есть только модуль, но нет функции. Как я могу решить эту проблему? >cmake_minimum_required (VERSION 3.10)
project ("pythonCpp")
add_subdirectory ("is_five")
is_five/cmakelists.txt
cmake_minimum_required (VERSION 3.10)
set(PYTHONPATH D:/python_10)
set(PYBINDPATH D:/python_10/Lib/site-packages/pybind11)
include_directories(${PYTHONPATH}/include ${PYBINDPATH}/include)
add_library(is_five SHARED is_five.cpp)
set_target_properties(is_five PROPERTIES SUFFIX ".pyd")
target_link_libraries(is_five ${PYTHONPATH}/libs/python310.lib)
< /code>
файл C ++: < /p>
#include
#include
#include
#include
#include
using namespace std;
namespace py = pybind11;
bool is_five(const vector& grid, int x, int y, int flag) {
// Direction vectors for: horizontal, vertical, diagonal, anti-diagonal
const array dx = {1, 0, 1, -1};
const array dy = {0, 1, 1, 1};
const int board_order = grid.size();
for (int dir = 0; dir < 4; dir++) {
int count = 1; // Start with 1 to count the current piece
// Check forward direction
for (int j = 1; j < 5; j++) {
int tx = x + j * dx[dir];
int ty = y + j * dy[dir];
if (tx < 0 || tx >= board_order || ty < 0 || ty >= board_order) {
break;
}
if (grid[ty][tx] == flag) {
count++;
} else {
break;
}
}
// Check backward direction
for (int j = -1; j > -5; j--) {
int tx = x + j * dx[dir];
int ty = y + j * dy[dir];
if (tx < 0 || tx >= board_order || ty < 0 || ty >= board_order) {
break;
}
if (grid[ty][tx] == flag) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
}
return false;
}
PYBIND11_MODULE(is_five, m) {
m.doc() = "pybind11 is_Five module";
m.def("is_five", &is_five, "A function that checks if a number is five");
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... with-cmake
Проблема с не удалось вызвать C ++ в здании Python с Cmake [закрыто] ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение