gpt4 book ai didi

c++ - 将 GTest 集成到我的项目时 gtest-printers.h 出错

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:34 25 4
gpt4 key购买 nike

我在构建我的项目时遇到以下错误

C:\gtest\gtest-1.6.0\include\gtest/gtest-printers.h(327) : error C2220: warning treated as error - no 'object' file generated
C:\gtest\gtest-1.6.0\include\gtest/gtest-printers.h(376) : see reference to function template instantiation 'void testing::internal::DefaultPrintTo<const void>(testing::internal::IsNotContainer,testing::internal::true_type,T *,std::ostream *)' being compiled
with
[
T=const void
]
C:\gtest\gtest-1.6.0\include\gtest/gtest-printers.h(416) : see reference to function template instantiation 'void testing::internal::PrintTo<To>(const T &,std::ostream *)' being compiled
with
[
To=const void *,
T=const void *
]
C:\gtest\gtest-1.6.0\include\gtest/gtest-printers.h(327) : warning C4826: Conversion from 'const void *' to 'testing::internal::UInt64' is sign-extended. This may cause unexpected runtime behavior.

我已经按如下方式初始化了 InitGoogleTest,

void startGTest()
{

char *option[] = { "test.exe", //it doesn't have meaning, just dummy
"--gtest_output=xml:gTestResults.xml" };
int argc = 2;

testing::InitGoogleTest(&argc, option);
RUN_ALL_TESTS();
}

更多信息,

int main(int argc, char** argv) 
{
char *option[] = { "test.exe", //it doesn't have meaning, just dummy
"--gtest_output=xml:gTestResults.xml" };
int myargc = 2;
testing::InitGoogleTest(&myargc, option);
RUN_ALL_TESTS();
getchar(); // keep console window open until Return keystroke
}

也抛出错误,

1>c:\...\gtestfactorial\gtestfactorial\gtestfactorial.cpp(4) : error C2220: warning treated as error - no 'object' file generated

如果编译器设置在项目属性中更改如下,

c/c++ -> 将警告视为错误 -> 否

然后我就可以构建并运行了。

最佳答案

我猜你会收到这个错误,因为你是在 64 位机器上针对 32 位平台进行交叉编译。我也明白了,这正是我正在做的。 documentation从微软说

This warning indicates that the compiler performed sign extension when a 32-bit pointer was cast to a 64-bit variable.

此警告默认情况下处于关闭状态,但 Google 将警告设为错误以确保任何人都可以使用他们的产品进行构建。您可以将其关闭,但 Microsoft 表示,如果在转换指针类型时发生错误,手动禁用符号扩展会更安全。

在我这边,我最终修改了生成错误的代码:

// Disable this code for cross compiling on 32 bits machine
// GTEST_OS_WINDOWS will always be defined when compiling on a Windows machine.
// GTEST_OS_TARGET_PLATFORM is defined by us when running CMake
#if GTEST_OS_WINDOWS && !GTEST_OS_TARGET_PLATFORM
*os << reinterpret_cast<const void*>(
reinterpret_cast<internal::UInt64>(p));
#else
*os << reinterpret_cast<const void*>(
static_cast<internal::UInt64>(
reinterpret_cast<internal::UInt32>(p)));
#endif

关于c++ - 将 GTest 集成到我的项目时 gtest-printers.h 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8699510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com