C言語からPython3で作った関数を呼び出せない
CからPython3の関数を呼び出そうとしていますがうまくコンパイルできません
pycall.cからpycall.pyのHiという関数を呼び出そうとしてしているのですが、リンクが失敗しているように見えます
どうすればいいのでしょうか?
コンパイル時のコマンド
clang -I`python3-config --prefix`/Headers -o pycall pycall.c
エラーメッセージ
pycall.c:4:36: warning: implicit declaration of function 'PyString_FromString' is invalid in C99 [-Wimplicit-function-declaration]
PyObject *pycallModuleString = PyString_FromString((char*)"pycall");
^
pycall.c:4:15: warning: incompatible integer to pointer conversion initializing 'PyObject *' (aka 'struct _object *') with an expression of type 'int' [-Wint-conversion]
PyObject *pycallModuleString = PyString_FromString((char*)"pycall");
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 warnings generated.
Undefined symbols for architecture x86_64:
"_PyFloat_AsDouble", referenced from:
_main in pycall-4fd2bb.o
"_PyImport_Import", referenced from:
_main in pycall-4fd2bb.o
"_PyObject_CallObject", referenced from:
_main in pycall-4fd2bb.o
"_PyObject_GetAttrString", referenced from:
_main in pycall-4fd2bb.o
"_PyString_FromString", referenced from:
_main in pycall-4fd2bb.o
"_Py_BuildValue", referenced from:
_main in pycall-4fd2bb.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
pycall.c
#include <Python.h>
int main() {
PyObject *pycallModuleString = PyString_FromString((char*)"pycall");
PyObject *pycallModule = PyImport_Import(pycallModuleString);
PyObject *HiFunction = PyObject_GetAttrString(pycallModule, (char*)"Hi");
PyObject *args = Py_BuildValue("()");
PyObject *myResult = PyObject_CallObject(HiFunction, args);
double result = PyFloat_AsDouble(myResult);
printf("%f\n", result);
}
pycall.py
def Hi():
print("Hi! I am Python3.6")
return 3.6