c++ 在项目中创建DLL,并调用
2024-10-21 11:44:53
创建DLL分为两种方法,先介绍第一种
一、创建DLL
(1)
// dll.h
#pragma once
//dll.h
#ifndef DLL_H_
#define DLL_H_
void printhello();
void callPython();
extern "C" _declspec(dllexport) void start();
#endif
//dll.cpp
#pragma once
#include <iostream>
#include <Python.h>
using namespace std;
void printhello() {
cout << "hello c++ dll !" << endl;
}
void callPython() {
Py_Initialize();
if (!Py_IsInitialized()) PyErr_Print();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('D:\\CodeDemo')");
PyObject* pModule = NULL;
PyObject* pFunc = NULL;
PyObject* pDict = NULL;
PyObject* pKey = NULL;
PyObject* pSubDict = NULL;
if (!(pModule = PyImport_ImportModule("hellopython"))) PyErr_Print();
if (!(pFunc = PyObject_GetAttrString(pModule, "hellopython"))) PyErr_Print();
PyObject_CallObject(pFunc, NULL);
}
extern "C" _declspec(dllexport)void start()
{
printhello();
callPython();
}
Configuration Properties-> General -> Configuration Type 改为dll。
Configuration Properties-> Linker-> General -> output file 按图修改。
然后Build->build soultion, 就可以在debug或者release目录下看到生成的DLL 文件
(2) 待补充
二、使用DLL文件
分为四步:
(1)
指到要引用的头文件路径。
(2)
输入链接中使用的lib文件
(3)
这是上一步lib文件的路径,
(4)把dll文件放在调用project的exe目录下即可
最新文章
- [问题记录.dotnet]取网卡信息报错";找不到";-WMI - Not found
- 【读书笔记】iOS网络-使用推送通知
- Web前端开发基础 第四课(认识CSS样式)
- [Effective JavaScript 笔记]第68条:使用promise模式清洁异步逻辑
- 【原创】jmeter3.0在beanshell中输入中文乱码以及字体大小的更改
- IDEA 从SVN检出项目相关配置
- ubuntu下ssh登陆阿里云服务器(ubuntu系统)中文乱码问题
- 加载不同的nib文件
- MongoDB简述
- NET Remoting 示例
- tomcat web.xml配置
- sed文本处理--文本行扩展与分割
- 【多线程】--生产者消费者模式--synchronized版本
- Apache POI 一键上传(导入excel文件到数据库)
- 前端基础之JavaScript
- Windows使用Gitblit搭建Git服务器
- [Swift]LeetCode506. 相对名次 | Relative Ranks
- golang开源项目qor快速搭建网站qor-example运行实践
- ARM 技术文档
- libctemplate——源码分析