python调用C++(仅限函数),ctypes调用so文件注意事项

单个.c文件生成.so:
cd /d D:\Program Files (x86)\MinGW\bin
gcc "D:\VisulStudio\gccSPA\gccSPA\SolarPosAngle.c" -fPIC -shared -o "D:\VisulStudio\gccSPA\gccSPA\gccSPA.so"
python中调用
from ctypes import *
aimHandle = CDLL("D:\VisulStudio\gccSPA\gccSPA\gccSPA.so")
aimHandle.main1.restype = c_double
aimHandle.main2.restype = c_double
aimHandle.main3.restype = c_double
aimHandle.main3.argtypes = (c_int, c_int, c_int, c_int, c_int, c_double, c_double)

SZA = aimHandle.main1()
SAA = aimHandle.main2()
print "1.SZA, SAA:",SZA, SAA

second_c = int(second)
second_c = c_int(second_c)
lat_c = c_double(lat)
lon_c = c_double(lon)
SZA = aimHandle.main3(year,dayNum,hour,minute,second_c,lat_c,lon_c)

一定要有函数输入输出类型的声明,int型不用转换,float和double类型需要进行转换,多个变量输入单个变量输出。

python位数要统一。如果出现win32,建议将python卸载,然后重新安装python和相应的库。

126的问题需要重新编译.so文件。

 

 

你可能感兴趣的:(python调用C++(仅限函数),ctypes调用so文件注意事项)