1.2vs支持python
参考:https://www.cnblogs.com/ming-4/p/10235709.html。
大概的步骤就是 进入“Visual Studio Installer”–》“修复”–》勾选“python开发” 和 “数据科学”
2 开发动态链接库demo
2.1开发代码
头文件代码开发:
#ifdef EXPORT_MY_DLL
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif
extern "C"
{
MY_API int IntAdd(int, int);
}
源文件代码开发:
#include "math_ops_h.h"
#define EXPORT_MY_DLL
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
int IntAdd(int a, int b)
{
int c = a + b;
cout << "a + b is " << c << endl;
return c;
}
选择64位,并debug生成项目:
设置python解释器:
2.2调用dll
添加python项目:
开发python调用代码:
import numpy as np
import ctypes
import warnings
import os
import platform
import sys
import os
# debug cpp
lib_path = r'D:\vs-cpp_projects\test-2\demo2-csdn\x64\Debug\demo2-csdn.dll'
_lib = ctypes.cdll.LoadLibrary(lib_path)
print(_lib.IntAdd(2,3))
执行测试:
。
3.1debug调试
1.在python代码前增加“input()” 函数:
2.先cmd命令执行,执行到input代码处
3.c++代码,按F5,再回到cmd,按enter
4.开始在c++代码处debug
4.其他
如果执行python代码出现““‘NoneType’ object has no attribute ‘write’””, 把python项目移除,用管理员启动vs,再重新添加python项目,即可。
参考1:https://blog.csdn.net/lacoucou/article/details/80066637
参考2:https://blog.csdn.net/qq_43629743/article/details/123594575?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167127965716800186535653%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=167127965716800186535653&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-1-123594575-null-null.142v68control,201v4add_ask,213v2t3_esquery_v1&utm_term=visual%20studio%20python%20%20%E8%B0%83%E8%AF%95dll