Dup Ver Goto 📝

CtypesBasics

PT2/lang/python/ctypes does not exist
To
24 lines, 57 words, 391 chars Page 'CtypesBasics' does not exist.

Absolute Minimal Example

This is on Linux. As I write this I'm not sure how things are different with Windows, Cygwin and macos.

The C (in mylib.c):

#include <stdio.h>

void print_number(int n) {
  printf("%d\n",n);
}

Compile with

gcc -shared -o mylib.so mylib.c

The Python

import ctypes
lib = ctypes.cdll.LoadLibrary("./mylib.so")
lib.print_number(42)