hello world with ctypes

Let's call a simple C code from Python with ctypes.

Compiling C code

test.c


#include <stdio.h>

void hello_world(void)
{
  printf("Hello, world!\n");
}

Compile this with the following command to make it a shared file.

gcc test.c -shared -fPIC -o libtest.so

Call from python

ctypes_test.py


import ctypes

libc = ctypes.cdll.LoadLibrary('./libtest.so')
libc.hello_world()

You can run it with the following command

python3 ctypes_test.py

Execution result

Hello, world!

Recommended Posts

hello world with ctypes
Hello, World with Docker
Hello world with flask
Draw hello world with mod_wsgi
Hello World with Flask + Hamlish
Until hello world with zappa
Hello world
Python starting with Hello world!
Hello, world! With virtual CAN communication
[Note] Hello world output with python
Hello World! By QPython with Braincrash
Pymacs hello world
cython hello world
Hello World and face detection with opencv-python 4.2
Hello World with Raspberry Pi + Minecraft Pi Edition
Hello World! By QPython with Brainfu * k
Hello World and face detection with OpenCV 4.3 + Python
Hello World with gRPC / go in Docker environment
web2py memo: Hello World
Hello world with full features of Go language
RabbitMQ Tutorial 1 ("Hello World!")
Say hello to the world with Python with IntelliJ
Hello World with nginx + uwsgi + python on EC2
Hello World on Django
Create a one-file hello world application with django
First python ① Environment construction with pythonbrew & Hello World !!
Django's first Hello World
Create a "Hello World" (HTTP) server with Tornado
Predicting Kaggle's Hello World, Titanic Survivors with Logistic Regression-Modeling-
Programming language in "Hello World"
Hello World in GO language
Hello World (beginners) on Django
Hello World with Google App Engine (Java 8) + Spring Boot + Gradle
Hello World with Google App Engine (Java 8) + Servlet API 3.1 + Gradle
Predicting Kaggle's Hello World, Titanic Survivors with Logistic Regression-Prediction / Evaluation-
Hello World with Google App Engine (Java 11) + Spring Boot + Gradle
Until Hello World with Flask + uWSGI + Nginx @ Sakura's VPS (CentOS 6.6)
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Let's do "Hello World" in 40 languages! !!
Start with Windows, not so scary Nim ① It's like hello world.
Introduction to TensorFlow --Hello World Edition
Hello world! (Minimum Viable Block Chain)
cout << "Hello, World! \ N" in python
Hello World in Flask [Appropriate memo]
Code: 2 "Hello World" in "Choregraphe-Python script"
Python #Hello World for super beginners
Introduction to Ansible Part 1'Hello World !!'
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
Re: Heroku life begin with Flask from zero - Environment and Hello world -
[Python] Web application from 0! Hands-on (2) -Hello World-
Python to remember only with hello, worlds
How to build Hello, World on #Nix
Hello world instead of localhost in Django
(For myself) Django_1 (Basic / Hello World / Template)
Flask tutorial (from installation to hello world)
A typed world that begins with Python
How to display Hello world in python
A web app that just does Hello World with Go's net / http package
I tried Hello World with 64bit OS + C language without using the library