I am learning TDD, using GoogleTest framework. I have successfully built Gtest and have been able to build and run the samples. However, when I tried a simple sample I wrote, I am getting compilation errors.
Here is the source and the build commands I used:
// ################################################
//proj1.h
#ifndef __SCRATCH_PROJ1_H
#define __SCRATCH_PROJ1_H
int addOne(int i);
#endif /*__SCRATCH_PROJ1_H */
// ################################################
//proj1.cpp
#include "proj1.h"
int addOne(int i){
return i+1;
}
// ################################################
//proj1_unittest.cpp
#include "proj1.h"
#include "gtest/gtest.h"
// Test Function
TEST(addOneTest, Positive) {
EXPECT_EQ(1,addOneTest(0)); // <- Line # 24
EXPECT_EQ(2,addOneTest(1)); // <- Line # 25
EXPECT_EQ(40320, addOneTest(40319)); // <- Line # 26
}
TEST(addOneTest, Negative) {
EXPECT_FALSE(addOneTest(-1)); // <- Line # 30
}
GTEST_API_ int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Console output:
g++ -isystem ${GTEST_DIR}/include -pthread -c /home/user1/scratch/proj1_unittest.cpp /home/user1/scratch/proj1_unittest.cpp: In member function ‘virtual void addOneTest_Positive_Test::TestBody()’: /home/user1/scratch/proj1_unittest.cpp:24:5: error: ‘addOneTest’ was not declared in this scope /home/user1/scratch/proj1_unittest.cpp:25:5: error: ‘addOneTest’ was not declared in this scope /home/user1/scratch/proj1_unittest.cpp:26:5: error: ‘addOneTest’ was not declared in this scope /home/user1/scratch/proj1_unittest.cpp: In member function ‘virtual void addOneTest_Negative_Test::TestBody()’: /home/user1/scratch/proj1_unittest.cpp:30:5: error: ‘addOneTest’ was not declared in this scope
Judging from the line numbers in the error messages, it seems that the EXPECT_* macros have not been defined - BUT, I have included gtest/gtest.h in the compilation unit.
What is causing these errors - and how do I fix it?