VSCode编译C++代码时找不到onnxruntime头文件求助
我是C++新手,使用VSCode和Ubuntu 18.04系统,尝试通过ONNX Runtime API部署神经网络模型。仅测试引入ONNX Runtime头文件的代码时出现编译错误,提示找不到onnxruntime/core/session/onnxruntime_cxx_api.h文件。
我的ONNX Runtime include目录为/home/jiahao/git/onnxruntime/include/,已将其添加到tasks.json和c_cpp_properties.json中,且Ctrl+点击头文件能跳转至正确文件,但编译仍失败。相关代码、错误信息及配置文件如下:
测试代码
#include <iostream> #include <ctime> #include <vector> #include <onnxruntime/core/session/onnxruntime_cxx_api.h> using namespace std; int main() { auto start_time = clock(); cout << "hello world" << endl; Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "test"); auto end_time = clock(); printf("Proceed exit after %.2f seconds\n", static_cast<float>(end_time - start_time) / CLOCKS_PER_SEC); printf("Done!\n"); return 0; }
编译错误输出
[test.cc 2023-01-05 10:08:28.381] ,,test.cc:4:10: fatal error: onnxruntime/core/session/onnxruntime_cxx_api.h: no such file or directory #include <onnxruntime/core/session/onnxruntime_cxx_api.h> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated.
配置文件
c_cpp_properties.json
{ "configurations": [ { "name": "linux-gcc-x64", "includePath": [ "${workspaceFolder}/**", "/home/jiahao/git/onnxruntime/include/", "/home/jiahao/git/onnxruntime/include/**" ], "compilerPath": "/usr/bin/gcc", "cStandard": "${default}", "cppStandard": "${default}", "intelliSenseMode": "linux-gcc-x64", "compilerArgs": [ "" ] } ], "version": 4 }
tasks.json
{ "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ 生成活动文件", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-I", "/home/jiahao/git/onnxruntime/include/", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "编译器: /usr/bin/g++" } ] }
解决办法
验证头文件实际存在
先确认/home/jiahao/git/onnxruntime/include/onnxruntime/core/session/onnxruntime_cxx_api.h这个文件是否真实存在。如果是从源码编译ONNX Runtime,需确保编译过程完成且头文件已生成到指定目录;如果是下载预编译包,确认包内包含完整的头文件结构。调整编译参数
尝试修改tasks.json中的-I参数,去掉路径末尾的斜杠,改为-I/home/jiahao/git/onnxruntime/include(合并成一个参数),避免空格或格式问题导致编译器无法识别路径。也可以添加-I/home/jiahao/git/onnxruntime/include/onnxruntime来直接指向头文件所在的子目录。终端手动编译排查
跳过VSCode配置,直接在终端执行编译命令测试:g++ -g test.cc -I /home/jiahao/git/onnxruntime/include -o test如果该命令成功,说明VSCode的tasks.json配置存在格式问题;如果仍失败,说明头文件路径或ONNX Runtime安装有问题,需重新检查文件结构或重新编译/下载ONNX Runtime。
确认版本匹配
确保使用的ONNX Runtime头文件与库文件版本一致,且对应Ubuntu 18.04的x64架构。源码编译时需指定正确的编译参数,确保生成适合当前系统的头文件和库。
内容的提问来源于stack exchange,提问作者Toby_Xiang

