如何用visual studio 2019配置OnnxRuntime
onnxruntime 配置方式
这里写自定义目录标题
一、下载onnx库文件
二、用visual studio 2019 解析nupkg包
三、用vs2019中的cmake来配置ONNXRunTime
一直以来都是用的linux系统,突然面对windows有点不知所措,这里记录一下,如何用vs2019配置OnnxRunTime
一、下载onnx库文件
这里有两种方案
1、直接下载include和lib文件
https://github.com/Microsoft/onnxruntime/releases/tag/v1.8.1
2、下载nupkg包
https://www.onnxruntime.ai/
选择适应自己的版本,博主选用的是cpu版本,如下链接
https://www.nuget.org/packages/Microsoft.ML.OnnxRuntime
得到一个microsoft.ml.onnxruntime.1.8.0.nupkg文件
这个包nupkg是visual studio 的NuGet Package的一个包文件 。
这个包里存放着onnx的头文件和库文件。
把这个包拷贝到 E:\git\cache路径
二、用visual studio 2019 解析nupkg包
1、首先用vs2019新建立一个项目
选择 工具->NuGet管理包->程序包管理控制台
然后输入
Install-Package Microsoft.ML.OnnxRuntime -Source E:\git\cache
这里E:\git\cache里面放着 microsoft.ml.onnxruntime.1.8.0.nupkg文件文件
注意,这里一定要建立个工程,才能执行以上的文件,否则会报
Install-Package : 找不到项目“Default”。
的错误。
完事后,在一下红框中的路径下,
有个\packages\Microsoft.ML.OnnxRuntime.1.8.0有几个文件
build下的include文件夹,就是onnxruntime的接口头文件
runtime文件夹下有着各个系统下的算法库文件。
这里,本文选择win-x64\native文件夹,这里放着OnnxRunTime的动态库和静态库。
这里,ONNX的nupkg文件已经解析完毕。
三、用vs2019中的cmake来配置ONNXRunTime
习惯了linux系统的我,还是更喜欢用cmake来配置工程
用vs2019建立一个cmake项目
CMakeLists.txt 里添加 ort 的 头文件和算法库的路径等
cmake_minimum_required (VERSION 3.8)
project (“CMakeProject1”)
include_directories(“E:/code/Microsoft.ML.OnnxRuntime.1.8.0/build/native/include”)
include_directories(“E:/opencv4.5.2/opencv/build/include”)
link_directories(“E:/code/Microsoft.ML.OnnxRuntime.1.8.0/runtimes/win-x64/native”)
link_directories(“E:/opencv4.5.2/opencv/build/x64/vc15/lib”)
#link_libraries(onnxruntime)
ADD_EXECUTABLE(hello CMakeProject1.cpp)
target_link_libraries(hello onnxruntime opencv_world452)
1
2
3
4
5
6
7
8
main.cpp文件夹,
#include
#include <assert.h>
#include
#include <onnxruntime_cxx_api.h>
#include
#include <opencv2/opencv.hpp>
int main(int argc, char* argv[]) {
Ort::Env env(ORT_LOGGING_LEVEL_WARNING, “test”);
Ort::SessionOptions session_options;
session_options.SetIntraOpNumThreads(1);
session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_BASIC);
#ifdef _WIN32
const wchar_t* model_path = L"E:/code/CMakeProject1/CMakeProject1/models/SENet_224.onnx";
#else
const char* model_path = “E:/code/CMakeProject1/CMakeProject1/models/SENet_224.onnx”;
#endif
Ort::Session session(env, model_path, session_options);
// print model input layer (node names, types, shape etc.)
Ort::AllocatorWithDefaultOptions allocator;
size_t num_input_nodes = session.GetInputCount();
std::cout << session.GetInputName(0, allocator) << std::endl;
std::cout << session.GetOutputName(0, allocator) << std::endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
运行调试时,可能会报找不到 onnxruntime.dll 的报错
这个主要是由于windows找不到库所在的路径导致的。
解决方案1:
将 onnxruntime.dll文件拷贝到可执行当前目录
解决方案2:
将 onnxruntime.dll 所在的文件夹添加到系统环境变量。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异