code

ctypes-초급

codestyles 2020. 9. 15. 07:51
반응형

ctypes-초급


ac 라이브러리를 파이썬 클래스로 "래핑"하는 작업이 있습니다. 문서는이 문제에 대해 매우 모호합니다. 고급 파이썬 사용자 만 ctypes를 구현할 것으로 기대하는 것 같습니다. 글쎄요 저는 파이썬 초보자이고 도움이 필요합니다.

단계별로 도움을 받으면 멋질 것입니다.

그래서 내 C 라이브러리가 있습니다. 어떡하죠? 어떤 파일을 어디에 두나요? 라이브러리를 어떻게 가져 옵니까? 파이썬에 "자동 랩"하는 방법이있을 수 있다고 읽었습니다.

(그런데 python.net에서 ctypes 튜토리얼을 수행했지만 작동하지 않습니다. 즉, 나머지 단계를 채울 수 있어야한다고 생각하고 있습니다.

사실 이것은 내가 그들의 코드에서 얻는 오류입니다.

File "importtest.py", line 1
   >>> from ctypes import *
   SyntaxError: invalid syntax

나는 이것에 대한 단계별 도움을 정말로 사용할 수 있습니다! 고마워 ~


여기에 빠르고 더러운 ctypes 튜토리얼이 있습니다.

먼저 C 라이브러리를 작성하십시오. 다음은 간단한 Hello world 예제입니다.

testlib.c

#include <stdio.h>

void myprint(void);

void myprint()
{
    printf("hello world\n");
}

이제 공유 라이브러리로 컴파일하십시오 ( 여기에있는 mac 수정 ).

$ gcc -shared -Wl,-soname,testlib -o testlib.so -fPIC testlib.c

# or... for Mac OS X 
$ gcc -shared -Wl,-install_name,testlib.so -o testlib.so -fPIC testlib.c

그런 다음 ctypes를 사용하여 래퍼를 작성합니다.

testlibwrapper.py

import ctypes

testlib = ctypes.CDLL('/full/path/to/testlib.so')
testlib.myprint()

이제 실행하십시오.

$ python testlibwrapper.py

그리고 당신은 출력을 볼 수 있습니다

Hello world
$

이미 라이브러리를 염두에두고 있다면 튜토리얼의 파이썬이 아닌 부분을 건너 뛸 수 있습니다. ctypes가 라이브러리를 /usr/lib또는 다른 표준 디렉토리 에 넣어 찾을 수 있는지 확인하십시오 . 이렇게하면 래퍼를 작성할 때 전체 경로를 지정할 필요가 없습니다. 이렇게하지 않으려면을 호출 할 때 라이브러리의 전체 경로를 제공 해야합니다ctypes.CDLL() .

더 포괄적 인 튜토리얼이있는 곳은 아니지만이 사이트의 특정 문제에 대한 도움을 요청하면 커뮤니티가 도움을 줄 것이라고 확신합니다.

추신 : 당신이 사용했기 때문에 Linux를 사용하고 있다고 가정합니다 ctypes.CDLL('libc.so.6'). 다른 OS를 사용하는 경우 상황이 약간 (또는 상당히 많이) 변경 될 수 있습니다.


Chinmay Kanchi의 대답은 훌륭하지만 C ++ 코드에 변수 / 배열을 전달하고 반환하는 함수의 예를 원했습니다. 다른 사람에게 유용 할 경우를 대비하여 여기에 포함하겠습니다.

정수 전달 및 반환

정수를 취하고 반환 된 값에 1을 더하는 함수에 대한 C ++ 코드,

extern "C" int add_one(int i)
{
    return i+1;
}

파일로 저장 test.cpp하고 필요한 extern "C"를 기록합니다 (C 코드의 경우 제거 할 수 있음). 이것은 Chinmay Kanchi 답변과 유사한 인수로 g ++를 사용하여 컴파일됩니다.

g++ -shared -o testlib.so -fPIC test.cpp

파이썬 코드를 사용 load_library로부터 numpy.ctypeslib파이썬 스크립트와 같은 디렉토리에 공유 라이브러리에 대한 가정의 경로,

import numpy.ctypeslib as ctl
import ctypes

libname = 'testlib.so'
libdir = './'
lib=ctl.load_library(libname, libdir)

py_add_one = lib.add_one
py_add_one.argtypes = [ctypes.c_int]
value = 5
results = py_add_one(value)
print(results)

This prints 6 as expected.

Passing and printing an array

You can also pass arrays as follows, for a C code to print the element of an array,

extern "C" void print_array(double* array, int N)
{
    for (int i=0; i<N; i++) 
        cout << i << " " << array[i] << endl;
}

which is compiled as before and the imported in the same way. The extra Python code to use this function would then be,

py_print_array = lib.print_array
py_print_array.argtypes = [ctl.ndpointer(np.float64, 
                                         flags='aligned, c_contiguous'), 
                           ctypes.c_int]
A = np.array([1.4,2.6,3.0], dtype=np.float64)
py_print_array(A, 3)

where we specify the array, the first argument to print_array, as a pointer to a Numpy array of aligned, c_contiguous 64 bit floats and the second argument as an integer which tells the C code the number of elements in the Numpy array. This then printed by the C code as follows,

1.4
2.6
3.0

Firstly: The >>> code you see in python examples is a way to indicate that it is Python code. It's used to separate Python code from output. Like this:

>>> 4+5
9

Here we see that the line that starts with >>> is the Python code, and 9 is what it results in. This is exactly how it looks if you start a Python interpreter, which is why it's done like that.

You never enter the >>> part into a .py file.

That takes care of your syntax error.

Secondly, ctypes is just one of several ways of wrapping Python libraries. Other ways are SWIG, which will look at your Python library and generate a Python C extension module that exposes the C API. Another way is to use Cython.

They all have benefits and drawbacks.

SWIG will only expose your C API to Python. That means you don't get any objects or anything, you'll have to make a separate Python file doing that. It is however common to have a module called say "wowza" and a SWIG module called "_wowza" that is the wrapper around the C API. This is a nice and easy way of doing things.

Cython generates a C-Extension file. It has the benefit that all of the Python code you write is made into C, so the objects you write are also in C, which can be a performance improvement. But you'll have to learn how it interfaces with C so it's a little bit extra work to learn how to use it.

ctypes have the benefit that there is no C-code to compile, so it's very nice to use for wrapping standard libraries written by someone else, and already exists in binary versions for Windows and OS X.

참고URL : https://stackoverflow.com/questions/5081875/ctypes-beginner

반응형