使用 VS 2015 及其新的内置 clang 工具集/项目模板,我无法成功构建 Google Test。我收到以下错误:
Error use of undeclared identifier 'chdir'; did you mean '_chdir'?
Error use of undeclared identifier 'fdopen'
Error use of undeclared identifier 'read'
Error use of undeclared identifier 'write'
Error use of undeclared identifier 'close'
Error use of undeclared identifier 'O_RDONLY'
Error use of undeclared identifier 'O_APPEND'
Error use of undeclared identifier 'dup'; did you mean '_dup'?
Error use of undeclared identifier 'creat'; did you mean '_creat'?
我注意到大多数声明都在这些 ANSI 检查 block 中:
#if !__STDC__
...
#endif
是否有项目设置或我可以更改的内容来解决这些方法?
最佳答案
我在使用 chdir 和 freopen 时遇到了类似的问题。
我将只发布我使用 VS2015 和 Clang 启动并运行 googletest 的步骤。
- 获取适用于 Windows 的 LLVM 快照构建。
http://llvm.org/builds/
(确保下载正确的版本(32 位/64 位))
这将安装最新版本的 clang(在编写 v3.9 时)。请注意,这是一个快照版本,而不是正式版本。
如果您不喜欢快照构建,可以尝试最新的发布版本。我没有测试它。我只是喜欢拥有最新的工具,尤其是当它们像 LLVM/Clang 这样快节奏的时候。
- 安装后,您应该会在 Visual Studio 项目属性中获得条目。
Properties
->General
->Platform Tools
->LLVM-vs2014
(和更多) (切换到LLVM-vs2014
)
我知道您要求使用 Microsoft CodeGen 的 Clang 3.7。你必须自己决定。
此外,我不喜欢对我没有编写或不知道的代码应用一些修复/更改。由于这对我来说很好,所以我没有进一步调查这个问题。
此时它可能已经为您工作。接下来的步骤描述了构建 googletest 库并将包含目录添加到项目中。
从 github 获取 googletest。 https://github.com/google/googletest
运行 cmake-gui 并配置 googletest 以便能够构建。
生成器:Visual Studio 14 2015 Win64(我只用了64bit,你也可以 尝试 32 位)
From the llvm documentation
(no link because not enough reputation: clang.llvm.org/docs/MSVCCompatibility.html):First, Clang attempts to be ABI-compatible, meaning that Clang-compiled code should be able to link against MSVC-compiled code successfully.
- 使用默认的原生编译器
源代码在哪里:(例如 C:\libs\googletest\googletest
)
(因为顶层目录下还有googlemock)
在哪里构建二进制文件:(例如 C:\libs\googletest\build
)
- 取消选中:
BUILD_SHARED_LIBS
(如果需要,构建共享库)CMAKE_CONFIGURATION_TYPES
: Debug and Release (其他也可以)
记住或更改:CMAKE_INSTALL_PREFIX
(例如C:\libs\googletest\install
)
Python 2.7 是由 cmake 发现的,尽管我很确定它不是必需的。
按配置和生成。
生成解决方案文件后,进入上面指定的目录 (在哪里构建二进制文件,例如
C:\libs\googletest\build
)并打开解决方案 gtest.sln。选择“调试解决方案配置”并右键单击“ALL_BUILD 和构建”。 完成后,右键单击“安装并构建”。这会创建之前指定的文件夹。
CMAKE_INSTALL_PREFIX
(例如C:\libs\googletest\install
) 在那里你可能想要更改库名称并添加一个 *d.lib 以防止文件被覆盖并作为调试版本的指示符。重复发布解决方案配置的步骤。 在
CMAKE_INSTALL_PREFIX
(例如C:\libs\googletest\install
)您应该找到一个包含目录和一个 lib 目录。在您的项目中,在属性 -> VC++ 目录下添加包含目录。
CMAKE_INSTALL_PREFIX<b>\include</b>
(例如C:\libs\googletest\install<b>\include</b>
)在您的项目中,在 Properties -> VC++ Directories 下添加 Library Directories。 CMAKE_INSTALL_PREFIX\lib(例如 C:\libs\googletest\install\lib)
在
Properties
下->Linker
->Input
->Additional Dependencies
(gtest.lib/gtestd.lib 取决于你的配置)
在那之后,我能够构建并运行我的测试。
关于c++ - 无法使用 Microsoft CodeGen 使用 Visual Studio 2015 和 Clang 3.7 构建 Google Test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34777647/