Python 调用 DLL
生活随笔
收集整理的這篇文章主要介紹了
Python 调用 DLL
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python 調用 DLL
一、C++ 編寫 DLL
1、.hpp 頭文件
// dll_test.hpp#ifdef DLL_TEST #define MY_API _declspec(ddllexport) // 當頭文件作為DLL工程編譯時(會用到DLL工程中的 .cpp 文件),設為導出。 #ELSE #define MY_API _declspec(dllimport) // 當DLL被其它工程調用時(不會用到DLL工程中的 .cpp 文件),設為導入。 #endif //需要被外界調用的類(父類) class MY_API my_class { public:// 類成員變量int x;// 類方法void func(int x); };// 函數,真正的函數名由編譯器決定 int MY_API add(int x, int y); // 函數,函數名不會被改變 extern "C" MY_API int add(int x, int y);2、.cpp 文件
// dll_test.cpp#define DLL_TEST // 定義宏,使編譯DLL工程時為導出 (結合頭文件看)#include <iostream> #include "dll_test.hpp" using namespace std;// 類方法實現 void MY_API my_class::func(int x) {cout << x << endl; } // 函數實現 int MY_API add(int x, int y) {return x+y; }二、Python 調用 DLL
1、ctypes 庫
- ctypes 庫用來調用 windows 的 dll / linux 的 so
- python 自帶 ctypes 庫,不需額外安裝
2、調用 DLL
- 第一步:用 c/c++,創建一個 dll
- 第二步:把生成的 .dll 文件拷貝到 .py 文件的同級目錄
- 第三步:使用 ctypes 庫調用 dll
GOOD LUCK!
總結
以上是生活随笔為你收集整理的Python 调用 DLL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习基础算法
- 下一篇: android.app.activity