google / googletest

GoogleTest - Google Testing and Mocking Framework

Home Page:https://google.github.io/googletest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Printers fail to link with scoped enums unless op<<() is defined.

fededevi opened this issue · comments

The linking process fails when a test use scoped enums with this error; an older version would fallback to print the hexadecimal values of the variable instead of failing.

Library is installed system-wide with cmake with 'cmake / make / make install'

main.o: in function `void testing::internal::RawBytesPrinter::PrintValue<EnumClass, 4ul>(EnumClass const&, std::ostream*)':
/usr/local/include/gtest/gtest-printers.h:269: undefined reference to `testing::internal::PrintBytesInObjectTo(unsigned char const*, unsigned long, std::ostream*)'

Tested on:
OS: Ubuntu 20.04.4 LTS
Main branch commit: 8ded48c
Compilers: clang version 10.0.0-4ubuntu1, g++ (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0
Build System: cmake version 3.16.3

This is the minimal code to reproduce the bug:

#include "gtest/gtest.h"

enum class EnumClass {
    VALUE_1,
    VALUE_2
};

/* Adding this fixes the problem
std::ostream& operator<<(std::ostream& os, const EnumClass& bar) {
    return os << std::to_string((int)bar);
}*/

TEST (A, B) {
    EXPECT_EQ(EnumClass::VALUE_1, EnumClass::VALUE_2);
}

int main( int argc, char *argv[] ) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

Adding the commented operator fixes the problem.

Error start happening with commit 0bf8ea3

I got a hint in #3578 and ended up finding that for my case, a third party library yaml-cpp 0.7.0 exports gtest 1.10 and gmock headers by default (until jbeder/yaml-cpp@0e6e98e turns it off), and the exported headers conflict with gtest 1.11 I'm using in test suites.