本文共 1233 字,大约阅读时间需要 4 分钟。
使 用 v s 2019 将 l i b t o r c h 打 包 为 d l l ( 环 境 : w i n 10 + v s 2019 + l i b t o r c h − c u d a 10.1 ) 使用vs2019将libtorch打包为dll(环境:win10+vs2019+libtorch-cuda10.1) 使用vs2019将libtorch打包为dll(环境:win10+vs2019+libtorch−cuda10.1)
前后有矛盾的问题,因为这篇文章不是一次写完的,我有空再修回来,我先休息一波
为什么是debug,因为我的libtorch是DeBug版本的
为什么是x64,因为x86性能支持不了
#pragma once#includeextern "C" __declspec(dllexport) void MyTest();
上面代码的功能是声明一个可被调用的函数“ MyTest()”,它的返回类型是void。
现在分析一下extern "C" __declspec(dllexport) void MyTest()
extern "C"
的作用是告诉编译器将被它修饰的代码按C语言的方式进行编译 __declspec(dllexport)
,此修饰符告诉编译器和链接器被它修饰的函数或变量需要从DLL导出,以供其他应用程序使用;
与其相对的还有一句代码是__declspec(dllimport)
此修饰符的作用是告诉编译器和链接器被它修饰的函数或变量需要从DLL导入,
最后是函数void MyTest()
,它就是需要被其他程序调用的函数。
#include#include "TestDLL.h"#include void MyTest(){ torch::Tensor tensor = torch::rand({ 5,3 }); std::cout << tensor << std::endl;}
(TestDLL.h)
//修改后的代码#pragma once#pragma comment(lib,"TestDLL.lib")#includeextern "C" __declspec(dllimport) void MyTest();
#include "TestDLL.h"int main(){ MyTest();}
转载地址:http://cwctz.baihongyu.com/