CMakeLists.txt
# project(工程名)
project(xxx) # add_library(链接库名称 SHARED 链接库代码)
add_library(xxx SHARED xxx.cpp)xxx.cpp
#include <iostream>
using namespace std; // c++ 结构体定义
struct struck_ { // 股票名,字符串
char * stock_code_; // 开盘价
double stock_open_;
}; // 声明为标准 C 格式导出的函数
extern "C" { // 参数接受结构体指针
int struck_function_(struck_ * struck_pointer_) {
cout << struck_pointer_->stock_code_ << endl;
cout << struck_pointer_->stock_open_ <<endl;
return 0;
}
}xxx.py
from ctypes import * # python 结构体定义
class py_struct_(Structure):
_fields_ = [("stock_code_", c_char_p), ("stock_open_", c_double)] # python 结构体实例化,初始化
py_struct_1 = py_struct_()
py_struct_1.stock_code_ = b"Hello world!"
py_struct_1.stock_open_ = 123456 # 取结构体指针
py_struct_1_pointer_ = byref(py_struct_1) # 获取 dll 句柄
h_dll_ = CDLL('C:\\Users\\Perelman\\.CLion2016.1\\system\\cmake\\generated\\xxx-34f96d0d\\34f96d0d\\Debug\\libxxx.dll') # 打印结果
print(h_dll_.struck_function_(py_struct_1_pointer_))
相关文章
- C++中嵌入python程序——参数传递
- 【转】C++中嵌入python程序——参数传递
- python嵌入到C++时发布的流程
- 将Python代码嵌入到C++程序
- 如何正确地将回调函数从swift传递到c++?
- UE4 打包C++项目到win32平台报错 could not find mspdbcore.dll
- python3.6:DLL load failed:找不到指定的模块(from PyQt5 import QtCore)
- win7 32位 安装opencv-python后,运行时提示 "from .cv2 import *: DLL load failed: 找不到指定的模块" 的解决办法
- 【转】C++中嵌入python程序——参数传递
- python调用c++传递数组的实例