将GTest与我的项目集成时,gtest-printers.h出错

时间:2012-01-02 10:00:57

标签: c++ visual-studio googletest

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

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 ++ - &gt;将警告视为错误 - &gt; NO

然后我可以构建并运行。

2 个答案:

答案 0 :(得分:0)

我猜你得到这个错误是因为你在64位机器上交叉编译32位平台。我也明白了,这正是我正在做的事情。微软的documentation

  

此警告表示编译器执行符号扩展时   一个32位指针被转换为64位变量。

默认情况下,此警告已关闭,但Google会将警告设为错误,以确保任何人都可以使用其产品进行构建。您可以将其关闭,但微软表示,如果在投射指针类型时发生错误,那么手动禁用符号扩展会更安全。

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

// 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

答案 1 :(得分:-1)

将整个错误日志复制到一个文件中,然后查找“错误”标记。你会发现一些相关的东西,如未声明的头文件或对象的前向声明,未定义的引用。此错误与gtest无关。