0

For a computation demonstration project in Linux and Python env, I need to call a C multiplication routine "matmult.c" but before I want to perform an important test if I could do another ctypes import from Windows. I have built a shared library named libdivide.so with MinGW (not MinGW-w64) and followed this post but without doing any configuration to indicate a compiler path to MinGW.

C:\base39\frede\Cpython_win32\setup.py

I really want to get some infos or some sites (I just read the ctypes official doc) to read to go further because my first question is: has anyone already tried this?

building firstly the shared lib and then run
python setup.py build_ext. In the meanwhile I will advance on Linux except if I have a return post.

My OS is Windows 10 64 bits.

Edit: I have installed MinGW on the root C:\ but the only time when I have tried to load a shared library from this compiler but outside msys.exe it was on another PC with Matlab R14(an eternity) and it had failed. Now I know that I would better try MinGW-w64 for R2021b but I could get the same error (below). My second question would be: do I need some extra knowledge about dll for getting this?

from distutils.core import setup, Extension
setup(name='divide',
        version='1.0',
        description='Python extension package for demo',
        ext_modules=[Extension('divide', ['dividefunc.c'],include_dirs=['include'])],
        packages=[''],  
        # fred yhe package is assumed to be at the root
        package_dir={'': 'lib'},
        #package_data = unused here
        )
import ctypes
lib = ctypes.cdll.LoadLibrary('divide.so')  #not sure

def divide_modulo(a,b):
    div=ctypes.c_int(0)
    rest=ctypes.c_int(0)
    lib.divide_int(a,b, ctypes.byref(div), c_types.byref(rest))
    return (div.value, rest.value)

#class DynamicArray(ctypes.Structure):
#   _fields_ = [("n", ctypes.c_int), ("r", ctypes.POINTER(c_types.c_int))]

./include/dividefunc.h
./lib/libdivide.so
./source/
./setup.py
./dividepy.py

Many thanks

(base39) C:\base39\frede\Cpython_win32>python setup.py build_ext

running build_ext

building 'divide' extension

error: Unable to find vcvarsall.bat

4
  • Have you ensured the python you are using was compiled with mingw's gcc? The default windows installer is compiled with visual studio. You will frequently get crashes if you try to use a python/library pair that were compiled with different compilers. Commented Oct 3, 2024 at 21:30
  • Welcome to SO. [SO]: Welcome to Stack Overflow. Check [SO]: How do I ask a good question? or [SO]: How to create a Minimal, Reproducible Example (reprex (mcve)) for more asking related details. Also, [JonSkeet.CodeBlog]: WRITING THE PERFECT QUESTION might be a good point to start. Commented Oct 27, 2024 at 11:18
  • Please state the situation you encounter, what are the steps that took you there, the actual and expected behaviors (copy / paste the outputs). Also one (main) question per question (post) please. So what is the problem? stackoverflow.com/a/78982617/4788546. What is the target Python interpreter that you want to load this module from? Commented Oct 27, 2024 at 12:45
  • Sorry I am back only now. The Python interpreter is a pre-compiled python 3.9.16 for windows. I have read the post (link you show) about cdll and I think I will hang on mingw(32) for now although I have currently problems with it (when compiling hello.c it shows $ /c/MinGW/bin/mingw32-gcc.exe hello.c cc1: error: unrecognized command-line option '-auxbase' cc1: error: too many filenames given; type 'cc1 --help' for usage cc1: error: CPU you selected does not support x86-64 instruction set) but that is another problem. Many thanks Commented Dec 17, 2024 at 0:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.