clion 导出dll给别的项目用,并且引入matplotcpp
dll生成一个mydll的dll文件
1.先做一个简单dll的测试下
cmake_minimum_required(VERSION 3.26)
project(untitled)
set(CMAKE_CXX_STANDARD 11)
add_library(untitled SHARED main.cpp)
main.h
void hello();
main.cpp
void hello()
{
std::cout << "Hello, mylib!" << std::endl;
}
ctrl+f9生成构建到目录下,生成libuntitled.dll
2.生成mydll用controller.cpp测试
cmake_minimum_required(VERSION 3.26)
project(mydll)
set(CMAKE_CXX_STANDARD 11)
add_library(mydll SHARED controller.cpp)
controller.cpp
#include "controller.h"
Controller::Controller(float _a_dist) : a_dist(_a_dist) {
}
controller.h
#include <vector>
class Controller {
private:
float a_dist;
public:
Controller(float a_dist_ = 1.0);
~Controller() = default;
};
生成libmydll.dll
3.clion调用
cmake_minimum_required(VERSION 3.26)
project(testpython)
set(CMAKE_CXX_STANDARD 11)
##python h, python数学计算
include_directories(D:\\app\\dev\\Python37\\include
D:\\app\\dev\\Python37\\Lib\\site-packages\\numpy\\core\\include)
##python lib 测试的dll
link_libraries(D:/app/dev/Python37/libs/python37.lib
C:/Users/andyz/CLionProjects/untitled/cmake-build-debug/libmydll.dll
C:/Users/andyz/CLionProjects/testdll/lib/libuntitled.dll)
add_executable(testpython main.cpp
matplotlibcpp.h)
目录结构
#include <iostream>
#include "Python.h"
#include "controller.h"
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
void hello();
int main() {
hello();
float a_dist = 1;
Controller controller(a_dist);
Py_Initialize();
PyRun_SimpleString("print('hi python')");
Py_Finalize();
std::cout << "Hello, World!" << std::endl;
return 0;
}
输出结果,一个是简单dll,一个是PyRun_SimpleString
4.qt生成的dll在vs 2022调用
参考这个文章https://blog.csdn.net/u010260855/article/details/22594191
#if defined(LOS_LIBRARY)
#define ADD_EXPORTS _declspec(dllexport)
#else
#define ADD_EXPORTS _declspec(dllimport)
#endif
class ADD_EXPORTS add
{
public:
add(void);
~add(void);
static int myadd(int a, int b)
{
return a+b;
}
};
#ifndef ADD_EXPORTS
#define ADD_EXPORTS//这个宏的名称与头文件中要一致
#endif
#include "add.h"
add::add(void)
{
}
add::~add(void)
{
}
代码目录需要放置los.dll