C++: Visual Studio can find included header through "Go to document", but build fails()
转载作者:bug小助手更新时间:2023-10-22 17:35:11254
I was attempting to restructure (filesystem-wise) a Visual Studio solution a little bit, and this solution uses an external library (specifically, Onnx Runtime; I use the NuGet OnnxRuntime Visual Studio extension Microsoft.ML.OnnxRuntime v1.12.0).
After restructuring, I took a look in the .vcxproj files of the various projects in my solutions, and ensured everything looked well, which it all seemed to.
重组后,我查看了解决方案中各个项目的.vcxproj文件,并确保一切看起来都很好。
When I try to build the project, however, I get this error:
然而,当我尝试构建项目时,我会遇到以下错误:
Error C1083 Cannot open include file: 'onnxruntime_cxx_api.h': No such file or directory
However, if I Ctrl+left click the include directive that causes this error (or alternatively: right click and choose "Go To Document"), it does manage to find it. The path of the opened file is: $(SolutionDir)\packages\Microsot.ML.OnnxRuntime.1.12.0\build\native\include\onnxruntime_cxx_api.h
(Where $(SolutionDir) is substtuted with the actual path to my solution file.)
(其中$(SolutionDir)被替换为我的解决方案文件的实际路径。)
My solution structure is a bit strange:
我的解决方案结构有点奇怪:
./solution/ ./solution.sln ./packages/ ./Microsoft.ML.OnnxRuntime.1.12.0/ ... ./sources/ ./projectA/ projectA .cpp and .hpp files ./projectB/ projectB .cpp and .hpp files ./vs_stuff/ ./projectA/ projectA Visual Studio files (vcxproj, vcxproj.users, packages.config, ...) ./projectB/ projectB .cpp and .hpp files (vcxproj, vcxproj.users, packages.config, ...)
I looked at all of the vs_stuff files and made sure that they do indeed point to the appropriate paths, and as far as I could tell, they do.
我查看了所有的vs_stuff文件,并确保它们确实指向了适当的路径,而且据我所知,它们确实指向。
Potentially crucial detail: this all started after I had attempted to restructure my solution. Initially it was the classic ./solution dir containing separate project directories, each with both sources and VS-related stuff; due to various requirements I had to change it to the structure described above, and the error resulted from this.
to my include directories or something, but that seems excessive and ugly and wasn't necessary before; plus, the IDE does find the header, so I'm not sure why the compiler doesn't.
the IDE does find the header, so I'm not sure why the compiler doesn't The compiler only looks in the same folder as your cpp source files or the system includes or the global includes or your project specific include directories. The compiler does not search all the places that the IDE does. These are independent.
@drescherjm, thanks for the input; interesting. I'd have assumed the IDE would try to ensure its behavior matches that of the compiler in that sense. Do you maybe have an idea how come the IDE could find the header file initially, even though this long path into the package wasn't directly specified in any file I could find? I struggle with finding any hint on Google...
Correction regarding my last comment: how the compiler* could find it.
关于我最后一条评论的更正:编译器*是如何找到它的。
I have the opposite problem, VS compiles and links fine, even IntelliSense and the Debugger work, but if I try to "Go to Document" of an include, it tells me it cannot find the file in the current source folder nor the system build paths. My .sln is generated by CMake, however, and the dependencies are installed with Conan, with the includes and libraries outside of the project folder. In the .vcxproj file, the external paths occur under <ClCompile>, <ResourceCompile>, <Midl>, and <Link>. Maybe you can check these? Do you have additional paths which make "Go to Document" work?
You may have inadvertently modified the configuration. When I moved the vcxproj file location, the line didn't exist anymore.
您可能无意中修改了配置。当我移动vcxproj文件位置时,该行已不存在。
To fix the problem:
要解决此问题,请执行以下操作:
1.Reinstall the Microsoft.ML.OnnxRuntime package.
1.重新安装Microsoft.ML.OnnxRuntime包。
Now you may get a NuGet package error. Open the vcxproj file with text. There will be multiple lines (at least 3) like '..\packages\Microsoft.ML.OnnxRuntime.1.12.0\build\native\Microsoft.ML.OnnxRuntime.targets. Delete them. And there are already right directories next to them <ImportProject="..\..\packages\Microsoft.ML.OnnxRuntime.1.12.0\build\native\Microsoft.ML.OnnxRuntime.props"
更多回答
Hi,@AnonA May I know if you have got any chance to check my answer?
我是一名优秀的程序员,十分优秀!