我一直在尝试按照一本书自己学习单元测试。
书中的代码使用C++11标准,并有这样一行:
auto variable = function(parameter);
当我第一次编译它时,我收到了这个警告:
warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]
没什么大不了的,我可以通过选中“项目”->“构建选项...”菜单中的以下框来解决这个问题:
[ ] Have g++ follow the C++ 11 ISO C++ language standard [-std=c++11]
但是现在,我在 gtest-port.h 中收到与 Google Mock 相关的新错误:
| | In function 'int testing::internal::posix::StrCaseCmp(const char*, const char*)':
|1719| error: '_stricmp' was not declared in this scope
| | In function 'char* testing::internal::posix::StrDup(const char*)':
|1721| error: '_strdup' was not declared in this scope
| | In function 'FILE* testing::internal::posix::FDOpen(int, const char*)':|
|1779| error: 'fdopen' was not declared in this scope
搜索这个问题对我来说收效甚微,但我确实尝试定义目标操作系统,因为它是建议的解决方案,以防它无法自动正确识别。在项目定义中添加 GTEST_OS_WINDOWS=1 和/或 GTEST_OS_WINDOWS_DESKTOP=1 没有任何改变。
我意识到在这种情况下,只需提供正确的类型而不是使用 auto 即可轻松解决此问题,但如果可能的话,我想找到解决方案。替换 auto 并且不检查 -std=c++11 选项会使代码按预期工作,因此库可以工作。
我在 Windows 中使用 Code::Blocks 13.12、MinGW/g++ 4.8.1-4 和 Google Mock 1.7。
感谢您的阅读=)
最佳答案
这里的答案在于缺少声明的函数:_stricmp
、_strdup
和fdopen
。前两个是 POSIX 函数 stricmp
和 strdup
的 Microsoft 版本。请注意,您指定使用 C++11 ISO 标准,该标准不包含 POSIX 标准中的项目。通过指定 --std=gnu++11
,您将告诉编译器接受 C++11 和 POSIX 以及 GNU 扩展的混合。
有趣的是,我无法在 Linux 上使用 GCC 4.8.2 复制此内容,因此在 Windows 上编译时,Google Mock header 中可能会发生其他情况。
关于C++:使用 Google Mock 和 Code::Blocks、MinGW 和 C++11 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26873400/