mingw如何制作动态库附python调用

1.mingw和msvc

bash 复制代码
g++ -fpic HelloWorld.cpp -shared -o test.dll

g++ -L . -ltest .\test.cpp

注意-L后面的.挨不挨着都行,-l不需要-ltest.dll,只需要-ltest

2.dll.cpp

extern "C" {
	__declspec(dllexport) int __stdcall add(int a, int b) {
		return a + b;
	}
}

3.dll.h

extern "C" {
	__declspec(dllexport) int __stdcall add(int a, int b);
}

4.test.c:必须要**#include "./HelloWorld.h",python则不需要**

#include <stdio.h>
#include "./HelloWorld.h"

extern int add(int a, int b);

int main(){
    printf("\n[%d]\n", add(12, 5));
    return 0;
}

5.python:main.py:经常找不到库,如何解决

1)绝对路径

2)os.add_dll_directory包含库的搜索路径

3)3.8以上版本,还是找不到就需要加上winmode=0

bash 复制代码
import ctypes  
import os
  
a = ctypes.WinDLL( 'test.dll' , winmode=0).add(12, 5)
print(a)

os.add_dll_directory("D:\\code\\thirdparty\\Chess\\cpp")
a = ctypes.WinDLL("test.dll").add(12, 5)
print(a)

6.python如果是cdecl就用CDLL()函数

相关推荐
※DX3906※1 小时前
cpp实战项目—string类的模拟实现
开发语言·c++
wjs20241 小时前
Nginx 安装配置指南
开发语言
美味小鱼1 小时前
实践Rust:编写一个猜数字游戏
开发语言·游戏·rust
Dr.勿忘2 小时前
C#面试常考随笔8:using关键字有哪些用法?
开发语言·unity·面试·c#·游戏引擎
dal118网工任子仪2 小时前
92,[8] 攻防世界 web Web_php_wrong_nginx_config
开发语言·php
wjs20242 小时前
SQLite Update 语句详解
开发语言
加油,旭杏2 小时前
【go语言】接口
开发语言·后端·golang
xianwu5433 小时前
反向代理模块jmh
开发语言·网络·数据库·c++·mysql
爱编程的小新☆3 小时前
Java篇之继承
java·开发语言
jk_1013 小时前
MATLAB中lineBoundary函数用法
开发语言·matlab