ctype,python通过ctype调用c/c++的dll动态库

ctype,python通过ctype调用c/c++的dll动态库
参考文档ctypes — Python 的外部函数库1.准备dll1.cpp端工程名cpp_native1.CMakeLists.txtcmake_minimum_required(VERSION3.14)project(cpp_native_sort_int_arr LANGUAGES CXX)set(CUR_TARGET_NAME ${PROJECT_NAME})# 创建动态库目标add_library(${CUR_TARGET_NAME}SHARED src/sort_int_arr.cpp)2.CMakePresets.json{version:6,configurePresets:[{name:mingw,displayName:GCC 9.2.0 mingw32,description:Using compilers: gcc, g,generator:MinGW Makefiles,binaryDir:${sourceDir}/build/${presetName},cacheVariables:{CMAKE_C_COMPILER:gcc,CMAKE_CXX_COMPILER:g,CMAKE_MAKE_PROGRAM:mingw32-make,CMAKE_BUILD_TYPE:Debug,CMAKE_EXPORT_COMPILE_COMMANDS:ON}}],buildPresets:[{name:mingwbuild,displayName:Build with MinGW,description:Build the project using the mingw configure preset.,configurePreset:mingw}]}3.src\sort_int_arr.h#ifndefSORT_INT_ARR_H#defineSORT_INT_ARR_H#ifdef__cplusplusexternC{#endif// 冒泡排序重复交换相邻逆序元素原地排序。voidbubble_sort(int*values,intlength);// 选择排序每轮选择未排序区间的最小值原地排序。voidselection_sort(int*values,intlength);#ifdef__cplusplus}#endif#endif4.src\sort_int_arr.cpp#includesort_int_arr.hvoidbubble_sort(int*values,intlength){for(inti0;ilength-1;i){for(intj0;jlength-1-i;j){if(values[j]values[j1]){inttempvalues[j];values[j]values[j1];values[j1]temp;}}}}voidselection_sort(int*values,intlength){for(inti0;ilength-1;i){intmin_indexi;for(intji1;jlength;j){if(values[j]values[min_index]){min_indexj;}}if(min_index!i){inttempvalues[i];values[i]values[min_index];values[min_index]temp;}}}2.编译cpp端cpp_nativecmake--presetmingw cmake--build--presetmingwbuild编译后可以看到如下输出libcpp_native_sort_int_arr.dll已被生成[ 50%] Building CXX object CMakeFiles/cpp_native_sort_int_arr.dir/src/sort_int_arr.cpp.obj [100%] Linking CXX shared library libcpp_native_sort_int_arr.dll [100%] Built target cpp_native_sort_int_arr2.python端创建一个test_sort_dll.py将dll复制到该py的同级目录下from_ctypesimportCFuncPtrimportosimportctypesfromctypesimportCDLLfromtypingimportList DLL_PATH:stros.path.join(os.path.dirname(__file__),libcpp_native_sort_int_arr.dll)# 1 加载dlldefload_dll(dllPath:str)-CDLL:returnctypes.CDLL(dllPath)# 2 获取c语言的排序函数defget_sort_func(func_name:str,dll:CDLL)-CFuncPtr:# // 冒泡排序重复交换相邻逆序元素原地排序。# void bubble_sort(int* values, int length);# // 选择排序每轮选择未排序区间的最小值原地排序。# void selection_sort(int* values, int length);func:CFuncPtrgetattr(dll,func_name)func.argtypes[ctypes.POINTER(ctypes.c_int),ctypes.c_int]func.restypeNonereturnfunc# 3.1 将 Python list 转换为 c语言的 int 数组defpython_list_to_c_arr(data:List[int])-ctypes.Array:# 第一步创建数组类型array_typectypes.c_int*len(data)# 创建一个可以容纳 5 个 int 的数组类型# 第二步解包并初始化arrayarray_type(*data)returnarray# 3.2 将 c语言的 int 数组转换为 Python listdefc_arr_to_python_list(c_array:ctypes.Array,length:int)-List[int]:# 将 ctypes int 数组转换为 Python list。return[c_array[i]foriinrange(length)]if__name____main__:dll:CDLLload_dll(DLL_PATH)bubble_sort:CFuncPtrget_sort_func(bubble_sort,dll)selection_sort:CFuncPtrget_sort_func(selection_sort,dll)# 测试数据集 - 包含各种边界情况test_cases:List[List[int]][[5,1,7,33,99],# 普通数据[3,1,4,1,5,9,2,6],# 重复元素[10,9,8,7,6,5,4,3,2,1],# 逆序数据[42],# 单元素[],# 空数组[-3,7,0,-1,5],# 负数和零]print(bubble_sort)fortest_caseintest_cases:print(排序前,test_case)length:intlen(test_case)c_array:ctypes.Arraypython_list_to_c_arr(test_case)bubble_sort(c_array,length)print(排序后,c_arr_to_python_list(c_array,length))print(selection_sort)fortest_caseintest_cases:print(排序前,test_case)length:intlen(test_case)c_array:ctypes.Arraypython_list_to_c_arr(test_case)selection_sort(c_array,length)print(排序后,c_arr_to_python_list(c_array,length))

最新新闻

日新闻

周新闻

月新闻