Search code examples
c++visual-studio-codeincludeonnxruntime

No such file or directory while compiling c++ in vscode include onnxruntime api header file


I'm very new to c++. I use VSCode and Ubuntu 18.04. I'm trying to use onnxruntime api in c++ to deploy my net model file.

Firstly I just tested including the onnxruntime api header file

#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;
}

And there's error in compiling my code. The output is

[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.

My onnxruntime directory is '/home/jiahao/git/onnxruntime/include/' and I have added it in tasks.json and c_cpp_properties.json.

Here is my 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
}

And here is my 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++"
        }
    ]
}

When I Ctrl+click the include line in my code, I will be directed to the correct onnxruntime_cxx_api.h. So I thought the include path is right but I don't know why I cann't compile my code.


Solution

  • I have solved this question. I downloaded the release version of onnxruntime. And in the release package I found header files and .so file. I added the include path in c_cpp_properties.json like this:

    {
        "configurations": [
            {
                "name": "linux-gcc-x64",
                "includePath": [
                    "${workspaceFolder}/**",
                    "/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/include/",
                    "/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/include/**",
                    "/usr/include/eigen3/"
                ],
                "browse": {
                    "path": [
                        "${workspaceRoot}",
                        "/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/lib/",
                        "/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/lib/**"
                    ]
                },
                "compilerPath": "/usr/bin/gcc",
                "cStandard": "${default}",
                "cppStandard": "${default}",
                "intelliSenseMode": "linux-gcc-x64",
                "compilerArgs": [
                    ""
                ]
            }
        ],
        "version": 4
    }
    

    And I added the include path, .so file path and lib name in tasks.json. Here I also used eigen3 library

    {
        "version": "2.0.0",
        "tasks": [
            {
                "type": "cppbuild",
                "label": "C/C++: g++ 生成活动文件",
                "command": "/usr/bin/g++",
                "args": [
                    "-fdiagnostics-color=always",
                    "-g",
                    "${file}",
                    "-I", "/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/include",
                    "-I", "/usr/include/eigen3",
                    "-L", "/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/lib",
                    "-l", "onnxruntime",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}",
                ],
                "options": {
                    "cwd": "${fileDirname}"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "编译器: /usr/bin/g++"
            }
        ]
    }
    
    Now I can compile and run my code correctly.