I have a simple C++ class with a method that I want to Call from a Google Test Fixture.
When I declare the method in the cpp file the compiler throws an undefined symbol error:
Class.h
class Class {
public:
double test() {
return 1.;
}
}
Class.cpp
double Class::test() {
return 1.;
}
GoogleTest.cpp
#include "Class.h"
class GoogleTest : public ::testing:Test {
protected:
Class c;
}
TEST_F(GoogleTest, TestIt) {
EXPECT_EQ(c.test(), 1.);
}
Meanwhile defining the method in the header I can compile like a charm:
Class.h
class Class {
public:
double test() {
return 1.;
}
}
GoogleTest.cpp
#include "Class.h"
class GoogleTest : public ::testing:Test {
protected:
Class c;
}
TEST_F(GoogleTest, TestIt) {
EXPECT_EQ(c.test(), 1.);
}
What is the cause of this behavior? I do not want to define all my methods in header.
The whole error:
Undefined symbols for architecture x86_64:
"Numeric::test()", referenced from:
ConstantsTest_Gamma_Test::TestBody() in 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[2]: *** [runUnitTests] Error 1