无法找到文件或目录,在使用VSCode编译C++时包含onnxruntime API头文件。

huangapple go评论81阅读模式
英文:

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 an 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 can't compile my code.

英文:

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 &lt;iostream&gt;
#include &lt;ctime&gt;
#include &lt;vector&gt;
#include &lt;onnxruntime/core/session/onnxruntime_cxx_api.h&gt;

using namespace std;

int main() {
  auto start_time = clock();
  cout &lt;&lt; &quot;hello world&quot; &lt;&lt; endl;
  Ort::Env env(ORT_LOGGING_LEVEL_WARNING, &quot;test&quot;);
  auto end_time = clock();
  printf(&quot;Proceed exit after %.2f seconds\n&quot;, static_cast&lt;float&gt;(end_time - start_time) / CLOCKS_PER_SEC);
  printf(&quot;Done!\n&quot;);
  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 &lt;onnxruntime/core/session/onnxruntime_cxx_api.h&gt;
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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

{
  &quot;configurations&quot;: [
    {
      &quot;name&quot;: &quot;linux-gcc-x64&quot;,
      &quot;includePath&quot;: [
        &quot;${workspaceFolder}/**&quot;,
        &quot;/home/jiahao/git/onnxruntime/include/&quot;,
        &quot;/home/jiahao/git/onnxruntime/include/**&quot;
      ],
      &quot;compilerPath&quot;: &quot;/usr/bin/gcc&quot;,
      &quot;cStandard&quot;: &quot;${default}&quot;,
      &quot;cppStandard&quot;: &quot;${default}&quot;,
      &quot;intelliSenseMode&quot;: &quot;linux-gcc-x64&quot;,
      &quot;compilerArgs&quot;: [
        &quot;&quot;
      ]
    }
  ],
  &quot;version&quot;: 4
}

And here is my tasks.json

{
	&quot;version&quot;: &quot;2.0.0&quot;,
	&quot;tasks&quot;: [
		{
			&quot;type&quot;: &quot;cppbuild&quot;,
			&quot;label&quot;: &quot;C/C++: g++ 生成活动文件&quot;,
			&quot;command&quot;: &quot;/usr/bin/g++&quot;,
			&quot;args&quot;: [
				&quot;-fdiagnostics-color=always&quot;,
				&quot;-g&quot;,
				&quot;${file}&quot;,
				&quot;-I&quot;, &quot;/home/jiahao/git/onnxruntime/include/&quot;,
				&quot;-o&quot;,
				&quot;${fileDirname}/${fileBasenameNoExtension}&quot;

			],
			&quot;options&quot;: {
				&quot;cwd&quot;: &quot;${fileDirname}&quot;
			},
			&quot;problemMatcher&quot;: [
				&quot;$gcc&quot;
			],
			&quot;group&quot;: {
				&quot;kind&quot;: &quot;build&quot;,
				&quot;isDefault&quot;: true
			},
			&quot;detail&quot;: &quot;编译器: /usr/bin/g++&quot;
		}
	]
}

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.

答案1

得分: 0

我已解决了这个问题。我下载了onnxruntime的发布版本。在发布包中,我找到了头文件和.so文件。我在c_cpp_properties.json中添加了包含路径,如下所示:

{
    "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
}

我还在tasks.json中添加了包含路径、.so文件路径和库名称。在这里,我还使用了eigen3库。

{
	"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++"
		}
	]
}

现在,我可以正确编译和运行我的代码。

希望这能帮助你。如果有其他问题,请告诉我。

英文:

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:

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

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

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

Now I can compile and run my code correctly.

huangapple
  • 本文由 发表于 2023年1月6日 10:25:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定