参考:
https://zhuanlan.zhihu.com/p/20152309
https://pgi-jcns.fz-juelich.de/portal/pages/using-c-from-python.html
eg:
1.
*.h
extern "C" int __attribute__((visibility("default"))) NLibMEx(int argc, char *argv[])
lib_test.py
[popexizhi:
这里是把python的argv转回给c使用了:)
相关参考

]
2.
https://zhuanlan.zhihu.com/p/20152309
https://pgi-jcns.fz-juelich.de/portal/pages/using-c-from-python.html
eg:
1.
*.h
extern "C" int __attribute__((visibility("default"))) NLibMEx(int argc, char *argv[])
lib_test.py
#-*- encoding:utf8 -*-
#!/usr/bin/python
#from ctypes import *
import sys
from ctypes import *
import ctypes
import time
if __name__ =="__main__":
print "*** " * 20
so = ctypes.CDLL("libNLibMEx.so")
so.NexusLibMainEntry.argtypes = (ctypes.c_int, ctypes.POINTER(ctypes.c_char_p))
print "*** " * 20
print "len(sys.argv) "+ str( len(sys.argv))
print "sys.argv "+str( sys.argv)
num_numbers = len(sys.argv)
array_type = ctypes.c_char_p * num_numbers
print array_type
so.NLibMEx(ctypes.c_int(num_numbers), array_type(*sys.argv))
while 1:
time.sleep(1)
$ python py_lib.py -cfg=a.cfg -help[popexizhi:
这里是把python的argv转回给c使用了:)
相关参考

]
2.
Basics
This is a short tutorial on using C from Python with the help of a ctypes wrapper module. Let's say we have a small C library for calculating sums and want to use it in Python.
sum.c:
int our_function(int num_numbers, int *numbers) {
int i;
int sum;
for (i = 0; i < num_numbers; i++) {
sum += numbers[i];
}
return sum;
}
Compile it:
cc -fPIC -shared -o libsum.so sum.c
Now to the main part, first the complete listing and then a line by line explanation: sum.py:
import ctypes
_sum = ctypes.CDLL('libsum.so')
_sum.our_function.argtypes = (ctypes.c_int, ctypes.POINTER(ctypes.c_int))
def our_function(numbers):
global _sum
num_numbers = len(numbers)
array_type = ctypes.c_int * num_numbers
result = _sum.our_function(ctypes.c_int(num_numbers), array_type(*numbers))
return int(result)
We import the ctypes module in the first line, then the shared library is loaded (usually you would want to include a check for the operating system here and name a dll instead, if running on windows). It is stored in the variable _sum and the functions in this library can be accessed as members (e.g. _sum.our_functions). In the next line, we set the argument types for the function. The function ctypes.POINTER creates a pointer datatype, so ctypes.POINTER(ctypes.c_int) is the ctypes datatype for int *. When using the functions, ctypes will check if the arguments fit to these types.
The real function definition follows in the next lines. We don't need the num_numbers argument as the length of Python sequences can be queried. This is the first thing we do in the function (after quickly stating that _sum is global to help the interpreter find it). Next, we need to define a ctypes array type, which is binary compatible to what the library expects. This can be done by 'multiplying' the type ctypes.c_int with an integer. To create an array with this type, we use:
array_type(*numbers)
The type expects each array element as an argument, so we use the asterisk notation with numbers. The result has the type ctypes.c_int and we want to return a python integer, therefore int(result) is returned.
[popexizhi:
1.array_type = ctypes.c_int * num_numbers 是py中的数组参数指针位置
2._sum.our_function(ctypes.c_int(num_numbers), array_type(*numbers))
这里的array_type(*numbers) 一定要使用指针方式,是在为int *numbers做地址位置,这里步骤1中应该就已经是c的转换了,看来python对c的写法还是和包容的:)
]
Using this wrapper module is quite straight-forward:
import sum
print sum.our_function([1,2,-3,4,-5,6])
没有评论:
发表评论