[Python] Problem with failed to call C++ in python building with Cmake

Discussão em 'Python' iniciado por Stack, Janeiro 31, 2025.

  1. Stack

    Stack Membro Participativo

    hi i have a big problem that trouble me all day.

    i'm using windows

    I paste all my code at the bottom of my question

    the picture is only to make you to see it more clearly!!!

    [​IMG]

    this is the files.

    and the two Cmake file:

    [​IMG]

    the left one is in the root of pythonCpp and the right one is in the is_five

    [​IMG]

    this is the is_five.cpp file.

    then i ran in the terminal:

    [​IMG]

    it seems to be all right

    However

    when i tried in python to call the c++ function is_five:

    [​IMG]

    everything went bad

    ** the problem is that **

    ** i cannot call my function is_five in python **

    >>> 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 "<stdin>", line 1, in <module>
    AttributeError: module 'is_five' has no attribute 'is_five'


    it seems that there is only the module but no function!

    how can i fix this problem?

    Please!

    together with the code instead of the img:

    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)


    the cpp file:

    #include <vector>
    #include <map>
    #include <iostream>
    #include <array>
    #include <pybind11/pybind11.h>

    using namespace std;
    namespace py = pybind11;

    bool is_five(const vector<vector<int> >& grid, int x, int y, int flag) {
    // Direction vectors for: horizontal, vertical, diagonal, anti-diagonal
    const array <int, 4> dx = {1, 0, 1, -1};
    const array <int, 4> 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");
    }


    **Please anyone can help?! **

    even a suggestion is helpful

    it is very curcial to me

    PLEASE

    Continue reading...

Compartilhe esta Página