capi.py
import myModule as capi
a,b=10,20
c=capi.Cal(a,b) #c=a+b
print(c)
Wenn du rennst
>python capi.py
30
capilib.c
#include <Python.h>
static PyObject* Cal(PyObject* self, PyObject* args){
int a,b,c;
if (!PyArg_ParseTuple(args, "ll",&a,&b)){
return NULL;
}
c=a+b;
return Py_BuildValue("l", c);
}
// Function Definition struct
static PyMethodDef myMethods[] = {
{ "Cal",Cal, METH_VARARGS, "Calc a+b"},
{ NULL }
};
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,"myModule","C API Module",-1,myMethods
};
PyMODINIT_FUNC PyInit_myModule(void){
return PyModule_Create(&myModule);
}
** Aufmerksamkeitspunkt ist **
int a,b,c;
if (!PyArg_ParseTuple(args, "ll",&a,&b)){
return NULL;
}
c=a+b;
return Py_BuildValue("l", c);
}
setup.py
from distutils.core import setup, Extension
setup(name = 'myModule', version = '1.0.0', \
ext_modules = [Extension('myModule', ['capilib.c'])])
** Erstellen einer Bibliothek **
capi>python setup.py install
running install
running build
running build_ext
building 'myModule' extension
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MT -IC:\ProgramData\Anaconda3\include -IC:\ProgramData\Anaconda3\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /Tccapilib.c /Fobuild\temp.win-amd64-3.7\Release\capilib.obj
capilib.c
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /nodefaultlib:libucrt.lib ucrt.lib /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\ProgramData\Anaconda3\libs /LIBPATH:C:\ProgramData\Anaconda3\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64" /EXPORT:PyInit_myModule build\temp.win-amd64-3.7\Release\capilib.obj /OUT:build\lib.win-amd64-3.7\myModule.cp37-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.7\Release\myModule.cp37-win_amd64.lib
Bibliotheksaufbau\temp.win-amd64-3.7\Release\myModule.cp37-win_amd64.lib und object build\temp.win-amd64-3.7\Release\myModule.cp37-win_amd64.Erstellen von exp
Ich generiere Code.
Die Codegenerierung ist abgeschlossen.
running install_lib
copying build\lib.win-amd64-3.7\myModule.cp37-win_amd64.pyd -> C:\ProgramData\Anaconda3\Lib\site-packages
running install_egg_info
Removing C:\ProgramData\Anaconda3\Lib\site-packages\myModule-1.0.0-py3.7.egg-info
Writing C:\ProgramData\Anaconda3\Lib\site-packages\myModule-1.0.0-py3.7.egg-info
Recommended Posts