I wanted to create a dll to use a function written in C from Python with ctypes, but I had a hard time

Overview

When I searched from Python to want to use a function written in C, I came to the conclusion that ctypes seems to be convenient this time, but when I got stuck in the first step (dll creation ~ read with python) Note.

The page that I referred to when ctypes

http://flow-developers.hatenablog.com/entry/2017/12/19/075319

environment

Windows10 (64bit) Anaconda 64bit (Python 3.6.4) Cygwin -gcc -shared hoge.c -o hoge.dll On the python side, the dll file created in lib=ctype.CDLL("hoge.dll") However, the error that python does not find the dll. I tried Cygwin with no use, but it certainly didn't work. MinGW64 Although the dll can be found [WinError 193] %1 is not a valid Win32 application DLL Error. Python is 64bit, MinGW is also 64bit, why. .. Then it was discovered that mingw's gcc is 32bit. .. .. Visual Studio 2017 I had no choice but to use VS. (X86_x64 Cross Tools Command Prompt for Visual Studio 2017.)

cl /LD hoge.c

The dll file is generated without difficulty! It can now be read on the Python side. I'm happy.

Recommended Posts

I wanted to create a dll to use a function written in C from Python with ctypes, but I had a hard time
I had a hard time with ImportError: DLL load failed in tensorflow 2.0
[Python] How to call a c function from python (ctypes)
I want to create a window in Python
I wanted to use the Python library from MATLAB
I want to work with a robot in python.
I had a hard time trying to access Hadoop3.0.0 from a browser (and Arch Linux)
Create a function in Python
I tried to create API list.csv in Python from swagger.yaml
Create a Mastodon bot with a function to automatically reply with Python
I made a package to filter time series with python
To return char * in a callback function using ctypes in Python
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
I tried to make a generator that generates a C # container class from CSV with Python
I also tried to imitate the function monad and State monad with a generator in Python
I tried to make a function to judge whether the major stock exchanges in the world are daylight saving time with python
A story that I wanted to do a function like before_action used in rails with django [Beginner learns python with a reference book]
A story that I had a hard time trying to create an "app that converts images like paintings" with the first web application
I want to use a wildcard that I want to shell with Python remove
Create folders from '01' to '12' with python
I wanted to create a smart presentation with Jupyter Notebook + nbpresent
I tried to create a list of prime numbers with python
How to create a heatmap with an arbitrary domain in Python
I wrote a function to load a Git extension script in Python
I wanted to solve the ABC164 A ~ D problem with Python
Pass a list by reference from Python to C ++ with pybind11
I wanted to use jupyter notebook with docker in pip environment (opticspy)
[Python] Explains how to use the range function with a concrete example
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
Create a tool to automatically furigana with html using Mecab from Python3
Use Python from Java with Jython. I was also addicted to it.
It's more recent, but I wanted to do BMI calculation with python.
Connect to postgreSQL from Python and use stored procedures in a loop.
[Outlook] I tried to automatically create a daily report email with Python
A memorandum because I stumbled on trying to use MeCab in Python
Python: I want to measure the processing time of a function neatly
Create a Python function decorator with Class
I want to use jar from python
I wanted to solve ABC160 with Python
I wanted to solve ABC159 in Python
Use C ++ functions from python with pybind11
I wanted to solve ABC172 with Python
I wanted to use the find module of Ansible2, but it took some time, so make a note
I wanted to create a program in Reverse Polish Notation in Python (determining if a string can be converted to a number)
I tried to create a class that can easily serialize Json in Python
I want to create a priority queue that can be updated in Python (2.7)
When I tried to create a virtual environment with Python, it didn't work
I made a familiar function that can be used in statistics with Python
I tried to easily create a fully automatic attendance system with Selenium + Python
I made a module in C language to filter images loaded by Python
[Python] I tried to get the type name as a string from the type function
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
How to use the C library in Python
I want to use MATLAB feval with python
To execute a Python enumerate function in JavaScript
How to create a function object from a string
Create a C array from a Python> Excel sheet
I wanted to solve NOMURA Contest 2020 with Python
How to create a JSON file in Python
I want to make a game with Python
Create a virtual environment with conda in Python