7

I have a Python Package (3.7) in which I've made a module, which makes use of a C-library for performance issues. This C-library is interfaced to through Ctypes. Now, I have a Makefile that compiles the C-library to a .so (in Windows using Cygwin minGW), however, to be able to easier share the package, I would like the library to be installed using at installation (setup.py), such that compiler/make does not have to be separately installed etc.

However, I have not been able to find a working method for distutils, or similar, to compile a C-library (not a Python Extension, pure C). The closest I have come to a working solution is this: https://stackoverflow.com/a/34830639/9863065

When doing as described in the above link, the .pyd is compiled without errors, however, when loading the file with Ctypes I get AttributeErrors when trying to access the defined functions in the C-library.

EDIT: I've made this minimum, reproducable example: https://github.com/mkielg11/ctypes_distribution

When using the above example, I get the following:

C:\...\ctypes_distribution>python setup.py build
running build
running build_py
running build_ext
building 'xclib' extension
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Ixpackage/deps/xclib/include/ -IC:\Anaconda3\include -IC:\Anaconda3\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /Tcxpackage/deps/xclib/cfile.c /Fobuild\temp.win-amd64-3.7\Release\xpackage/deps/xclib/cfile.obj
cfile.c
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\Anaconda3\libs /LIBPATH:C:\Anaconda3\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64" build\temp.win-amd64-3.7\Release\xpackage/deps/xclib/cfile.obj /OUT:build\lib.win-amd64-3.7\xclib.cp37-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.7\Release\xpackage/deps/xclib\xclib.cp37-win_amd64.lib
Generating code
Finished generating code

C:\...\ctypes_distribution>cd xpackage

C:\...\ctypes_distribution\xpackage>python xpackage.py
Example package python file
Traceback (most recent call last):
  File "xpackage.py", line 32, in <module>
    print('Result of example package function:', xpackage_function())
  File "xpackage.py", line 24, in xpackage_function
    get_magic_number_func = xlib.return_magic_number
  File "C:\Anaconda3\lib\ctypes\__init__.py", line 369, in __getattr__
    func = self.__getitem__(name)
  File "C:\Anaconda3\lib\ctypes\__init__.py", line 374, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'return_magic_number' not found
MRK
  • 546
  • 4
  • 6
  • 1
    Can you provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) with the full, specific error you are getting – Cory Kramer Jun 15 '20 at 12:15
  • I can try, but as there are a few different elements, it will take some time. Anything specifically that is unclear? – MRK Jun 15 '20 at 12:45
  • I've added a link to a repo on github, where I've made a minimum example. Due to the required multiple files, I thought it would help more in this way. – MRK Jun 15 '20 at 15:46
  • 1
    We build pure C libraries in [cocotb](https://github.com/cocotb/cocotb) using distutils. It's really messy. We did explore using [setuptools_dso](https://github.com/mdavidsaver/setuptools_dso), it's a setuptools extension for building pure C dynamic libraries, which seems to fit your requirements perfectly. – ktb Feb 21 '21 at 15:31
  • Thanks @ktb, I'll try and have a look at setuptoops_dso and your cocotb library! – MRK Feb 22 '21 at 17:21

0 Answers0