2014-07-25 2 views
1

파이썬에 C 확장을 시도했습니다. 내 문제는 C 함수 내에서 C 함수를 호출하여 C 확장을 작성한 것입니다. 예를 들어, 나는이 C 함수 안에서 pmd.h와 usb1024LS.h에서 C 함수를 사용하고있다. 스크립트를 실행하려고하면 "undefined symbol : hid_init"과 같은 오류가 발생합니다. 여기서 hid_init는 함수입니다. 나는 c 메인 프로그램에서 프로그램을 실행 해보았지만 제대로 작동했다. 확장자를 가진 다른 C 함수 내부에서 C 함수를 어떻게 호출합니까?파이썬 C 확장 내에서 C 함수를 호출하십시오.

감사합니다.

내 코드 : test.py - 테스트 스크립트 :

import ctypes 
import myTest_1024LS 

ctypes_findInterface = ctypes.CDLL('/home/oysmith/NetBeansProjects/MCCDAQ/usb1024LS_with_py/myTest_1024LS.so').findInterface 
ctypes_findInterface.restype = ctypes.c_void_p 
ctypes_findInterface.argtypes = [ctypes.c_void_p] 

ctypes_findInterface() 

setup.py :

from distutils.core import setup, Extension 

setup(name="myTest_1024LS", version="0.0", ext_modules = [Extension("myTest_1024LS", ["myTest_1024LS.c"])]) 

myTest_1024LS.c :

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <ctype.h> 
#include <sys/types.h> 
#include <asm/types.h> 
#include <python2.7/Python.h> 

#include "pmd.h" 
#include "usb-1024LS.h" 
#include "myTest_1024LS.h" 

void findInterface(void){ 
int interface; 
hid_return ret; 
ret = hid_init(); 
if (ret != HID_RET_SUCCESS) { 
     fprintf(stderr, "hid_init failed with return code %d\n", ret); 
     exit(1); 
} 

if ((interface = PMD_Find_Interface(&hid, 0, USB1024LS_PID)) >= 0) { 
     printf("USB 1024LS Device is found! interface = %d\n", interface); 
} else if ((interface = PMD_Find_Interface(&hid, 0, USB1024HLS_PID)) >= 0) { 
     printf("USB 1024HLS Device is found! interface = %d\n", interface); 
} else { 
     fprintf(stderr, "USB 1024LS and USB 1024HLS not found.\n"); 
     exit(1); 
} 
} 

PyDoc_STRVAR(myTest_1024LS__doc__, "myTes_1024LS point evaluation kernel"); 
PyDoc_STRVAR(findInterface__doc__, "find device"); 

static PyObject *py_findInterface(PyObject *self, PyObject *args); 

static PyMethodDef wrapper_methods[] = { 
{"findInterface", py_findInterface, METH_VARARGS, findInterface__doc__}, 
{NULL, NULL} 
}; 

PyMODINIT_FUNC initwrapper(void){ 
Py_InitModule3("wrapper", wrapper_methods, myTest_1024LS__doc__); 

} 

static PyObject *py_findInterface(PyObject *self, PyObject *args){ 

if(!PyArg_ParseTuple(args, "")){ 
    return NULL; 
} 
findInterface(); 
return 0; 
} 
+0

그런 다음 파이썬 함수를 다시 호출하여야한다 C 함수를 호출하는 파이썬 스크립트를 실행하려면 다음 findInterface() C의 기능도 인수도 같이 "선언"하는 반환 값을 가지고? 내가 맞다면 : C에서 내부 파이썬 함수를 다시 작성하는 것은 어떨까요? 어쨌든 당신은 C로 변환하여 sth를 얻고 싶습니다 (아마 속도인가요?), 그리고 나서 파이썬으로 다시 들어가면 그 재미를 망칠 수 있습니다. – Alfe

+0

아니요, myTest_1024LS.c에서 볼 수 있듯이 다른 C 함수를 호출하는 C 함수를 호출하는 Python 스크립트가 있습니다. C 함수 내부의 C 함수 호출은 usb 장치 드라이버에 대한 호출입니다. 나는 많은 드라이버 기능 때문에 C extenisons를 모든 드라이버 기능에 쓰고 싶지 않다. –

+0

나는 본다. USB 드라이버에 필요한 라이브러리가 코드에 연결되어 있다는 것을 어떻게 돌 봤습니까? – Alfe

답변

2

C의 확장을 구축하는 자신을 다른 공유 라이브러리와 링크되어야합니다. 어떤 라이브러리에 링크해야하는지 알려줘야합니다. setup.py 이 경우 최소한 hid_init() 함수를 내보내는 라이브러리. 자세한 내용과 예제는 Python 문서를 참조하십시오 : Building C and C++ Extensions with distutils. 두 번째 예제에는 추가 라이브러리를 확장 모듈에 연결하는 인수가 포함되어 있습니다.


ctypes "선언"잘못된 : void가 무효 포인터 (void*)와 동일하지 않습니다.

ctypes_findInterface.argtypes = [] 
ctypes_findInterface.restype = None 
관련 문제