zeffii / BlenderPythonRecipes

curated snippets for bpy (mostly for Blender <= 2.79 ), some changes are needed to maintain compatibility with 2.8+
GNU General Public License v2.0
121 stars 9 forks source link

executing C code from python (windows) #4

Open zeffii opened 7 years ago

zeffii commented 7 years ago

hello_world.c

#include <stdio.h>

void hello_world() {
    printf("Hello World!");
}

int main() {
    return 0;
}
import ctypes

path = "C:\\Users\\zeffi\\compiles\\hello.so"

lib = ctypes.cdll.LoadLibrary(path)
lib.hello_world()

"""
gcc -c hello_world.c      
>>> makes: hello_world.o

gcc -shared hello_world.o -o hello.so
>>> makes: hello.so
"""
zeffii commented 7 years ago

from http://python.net/crew/theller/ctypes/tutorial.html and https://stackoverflow.com/questions/4145775

import ctypes
pyarr = [1, 2, 3, 4]
arr = (ctypes.c_int * len(pyarr))(*pyarr)