C++ 最近谷歌测试中的彩色printf

C++ 最近谷歌测试中的彩色printf,c++,googletest,C++,Googletest,我使用的是相当过时的GoogleTest版本,并使用hack进行定制打印,如下所示: 我的源代码包含上面链接中的以下代码 namespace testing { namespace internal { enum GTestColor { COLOR_DEFAULT, COLOR_RED, COLOR_GREEN, COLOR_YELLOW }; extern void ColoredPrintf(GTestColor color

我使用的是相当过时的GoogleTest版本,并使用hack进行定制打印,如下所示:

我的源代码包含上面链接中的以下代码

namespace testing
{
 namespace internal
 {
  enum GTestColor {
      COLOR_DEFAULT,
      COLOR_RED,
      COLOR_GREEN,
      COLOR_YELLOW
  };

  extern void ColoredPrintf(GTestColor color, const char* fmt, ...);
 }
}
#define PRINTF(...)  do { testing::internal::ColoredPrintf(testing::internal::COLOR_GREEN, "[          ] "); testing::internal::ColoredPrintf(testing::internal::COLOR_YELLOW, __VA_ARGS__); } while(0)

我已经在我的项目中将GoogleTest的源代码更新为主版本,现在我有链接错误,说ColoredPrintf没有定义

error LNK2019: unresolved external symbol "void __cdecl testing::internal::ColoredPrintf(enum testing::internal::`anonymous namespace'::GTestColor,char const *,...)" (?ColoredPrintf@internal@testing@@YAXW4GTestColor@?A0x313d419f@12@PEBDZZ) referenced in function
研究fresh
gtest.cc
表明,他们已将
GTestColor
更改为enum类,并将其放入命名空间内的匿名命名空间
testing::internal

我已将源代码中的代码段更改为:

namespace testing {
    namespace internal
    {
        enum class GTestColor { kDefault, kRed, kGreen, kYellow };

        extern void ColoredPrintf(GTestColor color, const char* fmt, ...);
    }
}
作为一个快速修复,我在
gtest.cc
中删除了
namespace{…}
周围的
GTestColor


问题:是否可以避免编辑
gtest.cc
,并且仍然可以访问其功能?

您不能访问编译单元之外的匿名命名空间中的成员。另见

根据您的用例,备选方案可能是:

  • 用于替换标准输出
  • 通过修改输出的脚本传递输出
  • 让Google测试生成XML或JSON报告,并基于XML/JSON报告生成输出

或在googletest存储库中创建PR,使此函数公开可用。@wl2776这不是必需的,因为您可以使用标准事件侦听器API获得相同的结果。我已更新了我的问题,以包括此选项。