Я использую Windows
Когда я попробовал в Python, чтобы вызвать функцию C ++ is_five < /code> Все прошло плохо < /p>
>>> 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>
Кажется, что есть только модуль, но нет функции! < /p>
Как я могу решить эту проблему? < /p>
pythoncpp/cmakelists.txt
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>
Файл CPP: < /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++
Программы на C++. Форум разработчиков
1738347707
Anonymous
Я использую Windows
Когда я попробовал в Python, чтобы вызвать функцию C ++ is_five < /code> Все прошло плохо < /p>
>>> 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>
Кажется, что есть только модуль, но нет функции! < /p>
Как я могу решить эту проблему? < /p>
[b] pythoncpp/cmakelists.txt [/b]
cmake_minimum_required (VERSION 3.10)
project ("pythonCpp")
add_subdirectory ("is_five")
[b]is_five/cmakelists.txt[/b]
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>
Файл CPP: < /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");
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79403268/problem-with-failed-to-call-c-in-python-building-with-cmake[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия