Jak wspomniano w odpowiedzi Alex Martelli (choć link w wiadomości listy dyskusyjnej został uszkodzony, powinien być https://docs.python.org/extending/embedding.html#pure-embedding) .. Model C sposobu wywoływania ..
print urllib.urlopen("http://google.com").read()
- Dodaj Python. ramy do projektu (prawy przycisk myszy
External Frameworks..
, Add > Existing Frameworks
. ramy ww /System/Library/Frameworks/
- Dodaj
/System/Library/Frameworks/Python.framework/Headers
do "Header Search Path" (Project > Edit Project Settings
)
Poniższy kod powinien działać (choć nie jest to chyba najlepszy kod kiedykolwiek napisane ..)
#include <Python.h>
int main(){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Py_Initialize();
// import urllib
PyObject *mymodule = PyImport_Import(PyString_FromString("urllib"));
// thefunc = urllib.urlopen
PyObject *thefunc = PyObject_GetAttrString(mymodule, "urlopen");
// if callable(thefunc):
if(thefunc && PyCallable_Check(thefunc)){
// theargs =()
PyObject *theargs = PyTuple_New(1);
// theargs[0] = "http://google.com"
PyTuple_SetItem(theargs, 0, PyString_FromString("http://google.com"));
// f = thefunc.__call__(*theargs)
PyObject *f = PyObject_CallObject(thefunc, theargs);
// read = f.read
PyObject *read = PyObject_GetAttrString(f, "read");
// result = read.__call__()
PyObject *result = PyObject_CallObject(read, NULL);
if(result != NULL){
// print result
printf("Result of call: %s", PyString_AsString(result));
}
}
[pool release];
}
także this tutorial jest dobre
Duplikat: http://stackoverflow.com/questions/49137/calling -python-from-ac-program-for-distribution; http://stackoverflow.com/questions/297112/how-do-i-use-python-libraries-in-c. Możesz osadzić Pythona w dowolnej aplikacji. –
@ S.Lott To nie jest duplikat; te pytania dotyczą C++, a nie Objective-C. Chociaż możesz używać Objective-C++ do miksowania C++ i Objective-C, musisz sam zawinąć cały kod C++ w klasach Objective-C, jeśli chcesz użyć ich jako klasy Objective-C. –