Call your own C language shared library from Python using ctypes

I found out how to call and use a C language library in Python, so make a note of it.

Environment

Compile environment: gcc included in Xcode 10.2. Python:3.7.2

Create a shared library in C

Create a library (libmyadd.so) to call from Python. The contents of the library consist of integer type add functions.

math.c


int add(int a, int b){ return (a + b); }

To create a shared library, run the following command.

gcc -shared -fPIC -o libmyadd.so math.c

This will create libmyadd.so.

Use libmyadd.so from Python

The python code for calling this from Python is as follows. It is assumed that the location of the library is the same directory as the source code.

main.py


import ctypes as ct

libc = ct.cdll.LoadLibrary("./libmyadd.so")

if (__name__ == "__main__"):
    print(libc.add(1, 2))

Others (Use your own library in C

As an aside, make a note when calling from C language. It is assumed that the location of the library is the same directory as the source code.

main.c


#include <stdio.h>
  
int add(int a, int b);

int main(void)
{
  printf("%d¥n", add(1, 2));
  return 0;
}

Compile this as follows.

gcc -L./ -lmyadd -o main main.c

To do this, do the following:

LD_LIBRARY_PATH=./myadd ./main

Summary

This time, I used a package called ctypes of Python to call a shared library of C language that I made from Python. There is still a vague understanding, so I would like to investigate and update the article next time.

The created file is posted on GitHub.

Reference

How to create and dynamically link shared libraries on Linux: smart space Python Document 3.7

Recommended Posts

Call your own C language shared library from Python using ctypes
Call your own C library with Go using cgo
Let's call your own C ++ library with Python (Preferences)
Call c language from python (python.h)
Use a scripting language for a comfortable C ++ life 4-Use your own C ++ library from a scripting language-
Call your own python module from the ROS package
[Python] How to call a c function from python (ctypes)
Call C language functions from Python to exchange multidimensional arrays
Call C from Python with DragonFFI
Call popcount from Ruby / Python / C #
Call C / C ++ from Python on Mac
[Python] Register your own library on PyPI
Publish your own Python library with Homebrew
Generate C language from S-expressions in Python
C language to see and remember Part 2 Call C language from Python (argument) string
C language to see and remember Part 1 Call C language from Python (hello world)
C language to see and remember Part 4 Call C language from Python (argument) double
C language to see and remember Part 5 Call C language from Python (argument) Array
[Python] Implement your own list-like class using collections.UserList
Linux C / C ++ Build your own library creation environment
Call Python library for text normalization from MATLAB
Call a Python script from Embedded Python in C ++ / C ++
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
I tried using the Python library from Ruby with PyCall
Until you can install your own Python library with pip
Run the intellisense of your own python library with VScode.
Go language to see and remember Part 8 Call GO language from Python
Pass OpenCV data from the original C ++ library to Python
Flatten using Python yield from
Call CPLEX from Python (DO cplex)
Install mecab on Sakura shared server and call it from python
Cross-compile for Raspberry Pi Zero on Debian-Create your own shared library
MobileNetV2-SSD Lite's pre-processing, post-processing, etc. are partly converted into a C ++ shared library and called from Python.
Call Matlab from Python to optimize
Call a Python function from p5.js.
[Python] Make your own LINE bot
Using Rstan from Python with PypeR
Notes on using MeCab from Python
Introduction to Protobuf-c (C language ⇔ Python)
Using Cloud Storage from Python3 (Introduction)
Tips for calling Python from C
Execute Python code from C # GUI
Call python from nim with Nimpy
Python to switch from another language
Run Ansible from Python using API
Precautions when using phantomjs from python
Try using Amazon DynamoDB from Python
[python] How to add RDF triples to your own Fuseki server using rdflib