Search code examples
pythonc++terminaldependenciesctypes

c++ python ctypes dependency issues


I am trying to make my python program use c++ code but I get dependency errors.

Here is the c++ code that I converted into a dll file:

#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

extern "C" {
    int b_search(int* arr, int size, int v) {
        auto it = lower_bound(arr, arr + size, v);
        int outp = distance(arr, it);
        return outp;
    }
}

vector<priority_queue<int>> Vpqs;

extern "C" {
    int p_queue(int* arr, int size) {
        priority_queue<int> outp;
        for (int i = 0; i < size; i++) {
            outp.push(arr[i]);
        }
        Vpqs.push_back(outp);
        return Vpqs.size() - 1;
    }
    int empty_p_queue() {
        priority_queue<int> outp;
        Vpqs.push_back(outp);
        return Vpqs.size() - 1;
    }
    void pq_push(int pq, int item) {
        Vpqs[pq].push(item);
    }
    int pq_pop(int pq) {
        int outp = Vpqs[pq].top();
        Vpqs[pq].pop();
        return outp;
    }
    int pq_size(int pq) {
        return Vpqs[pq].size();
    }
    int* pq_to_array(int pq) {
        priority_queue<int> pq_copy = Vpqs[pq];
        int pq_size = pq_copy.size();
        int* outp = new int[pq_size];
        for (int i = 0; i < pq_size; ++i) {
            outp[i] = pq_copy.top();
            pq_copy.pop();
        }
        return outp;
    }
}

int main() {
    return 0;
}

Then I tried converting it into a dll file. I used this command in the terminal:

g++ -fPIC -shared -o qolc.dll clibrary.cpp -lstdc++

And then I tried importing it with python ctypes:

cppcode = ctypes.CDLL(path.dirname(path.abspath(__file__)) + '\\QoL\\clibrary\\clibrary\\qolc.dll')`

I got this error:

Traceback (most recent call last):
  File "D:\Python\game_engine\.venv11\Lib\site-packages\QoL.py", line 28, in <module>
    cppcode = ctypes.CDLL(path.dirname(path.abspath(__file__)) + '\\QoL\\clibrary\\clibrary\\qolc.dll')
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\ctypes\__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: Could not find module 'D:\Python\game_engine\.venv11\Lib\site-packages\QoL\clibrary\clibrary\qolc.dll' (or one of its dependencies). Try using the full path with constructor syntax.

I tried doing some research but I didn't find the solution.

The problem shouldn't be with the location of the dll file:

print(Path(path.dirname(path.abspath(__file__)) + '\\QoL\\clibrary\\clibrary\\qolc.dll').exists())
>>>True

Also, I tried removing parts of the c++ code to see if there are problems only with specific parts and it seems like if I remove the priority queue section of the code and only leave the main and lower bound functions the rest works, so the problem might be with the usage vectors or priority queues even though just including them didn't raise any errors.


Solution

  • I solved the problem by adding the flag '-static' into the g++ terminal command to remove the dependencies.