无法打开包含文件 - 'gtest.h' - 没有此类文件或目录

Cannot open include file - 'gtest.h' - No such file or directory

本文关键字:文件 gtest 包含      更新时间:2023-10-16

我正在尝试在Visual Studio中构建gtest,但似乎在获取引用和包含为项目正确指定的引用时遇到了一些问题。

错误 C1083:无法打开包含文件:"gtest/gtest.h":没有此类文件或目录 c:\gtest-1.6.0\src\gtest-all.cc

1>InitializeBuildStatus:
1>  Touching "DebugGTestBuild.unsuccessfulbuild".
1>ClCompile:
1>  gtest-all.cc
1>c:gtest-1.6.0srcgtest-all.cc(40): fatal error C1083: Cannot open include file: 'gtest/gtest.h': No such file or directory
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.61

对于项目,我在项目项目页面> C/C++>附加包含目录列表中添加了对以下内容的引用:

C:\gtest-1.6.0C:\gtest-1.6.0\srcc:\gtest-1.6.0\includeC:\gtest-1.6.0\include\gtest

但我似乎缺少其他一些包含或可能没有正确设置,并希望在解决此问题方面提供一些帮助,并学习如何为将来执行此操作。

附言。 切换自

#include "gtest/gtest.h"
// The following lines pull in the real gtest *.cc files.
#include "src/gtest.cc"
#include "src/gtest-death-test.cc"
#include "src/gtest-filepath.cc"
#include "src/gtest-port.cc"
#include "src/gtest-printers.cc"
#include "src/gtest-test-part.cc"
#include "src/gtest-typed-test.cc"

#include <gtest/gtest.h>
// The following lines pull in the real gtest *.cc files.
#include <src/gtest.cc>
#include <src/gtest-death-test.cc>
#include <src/gtest-filepath.cc>
#include <src/gtest-port.cc>
#include <src/gtest-printers.cc>
#include <src/gtest-test-part.cc>
#include <src/gtest-typed-test.cc>

不是解决方案。我已经试过了,但它不起作用。

检查完整的编译输出,以查看这些包含目录是否被合并到编译中。它应该看起来像这样:

...
-Ic:gtest-1.6.0 -Ic:gtest-1.6.0src -Ic:gtest-1.6.0include -Ic:gtest-1.6.0includegtest
...

您是否也在这些目录中查找过该文件?不要忘记,当您将其包含在目录中时,您必须在以下目录中查找gtest.h

c:gtest-1.6.0gtest
c:gtest-1.6.0srcgtest
c:gtest-1.6.0includegtest
c:gtest-1.6.0includegtestgtest

(请注意gtest子目录,因为您使用#include "gtest/gtest.h"

如果在"属性页"中查找文件 gtest-all.cc,则其"其他包含目录"字段应显示:

..;..include;%(AdditionalIncludeDirectories)

如果您使用了提供的 MSVC\gtest.sln,或者:

C:/gtest-1.6.0/include;C:/gtest-1.6.0;%(AdditionalIncludeDirectories)

如果使用 CMake 创建 VS 解决方案。

如果该字段为空,则它不会获取您为整个项目设置的目录,因为它们是通过 %(AdditionalIncludeDirectories) 变量应用的。 如果是这种情况,可能值得重新下载并重新开始,因为构建不再处于良好状态。