Reputation: 1591
I am not sure if I setup my gtest environment correctly. When I do plain TEST
with EXPECT_EQ
stuff, it's all good. However, when I try fancier stuff like TEST_F
, the linker complains.
Source Code:
class MyTest : public testing::Test
{
protected:
static const int my_int = 42;
};
TEST_F(MyTest, test)
{
EXPECT_EQ(my_int, 42);
}
And this gives
Undefined symbols for architecture x86_64:
"MyTest::my_int", referenced from:
MyTest_test_Test::TestBody() in instruction_test.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [tests/tests/run_tests] Error 1
make[2]: *** [tests/tests/CMakeFiles/run_tests.dir/all] Error 2
make[1]: *** [tests/tests/CMakeFiles/run_tests.dir/rule] Error 2
make: *** [run_tests] Error 2
Any idea why this happens?
Upvotes: 0
Views: 1093
Reputation: 1829
This not a problem from googletest, it is the semantic from C++.
The reason: We can only call static class members on the class and not on an object of the class. And that is possible, even if no instance exist. That's why every static member instance must be initialize, usually in the cpp file.
Upvotes: 1
Reputation: 1591
I managed solve the problem, but I don't know why it works this way:
So before I use static const int my_int
, I have to declare it again outside of the MyTest class:
class MyTest : public testing::Test
{
protected:
static const int my_int = 42;
};
const int MyTest::my_int;
TEST_F(MyTest, test)
{
EXPECT_EQ(my_int, 42);
}
Upvotes: 1