这是我所做的,您可以根据需要进行调整。我下载了 gtest-1.6.0.zip(来自releases https://github.com/google/googletest/releases页)在我的 Linux 盒子上进入 ~/Downloads ,完整输入的是 /home/me/Downloads/
将 gtest-1.6.0.zip 的内容解压到 ~/Downloads/gtest-1.6.0/
cd /home/me/Downloads
unzip gtest-1.6.0.zip
构建 gtest 库,因为您需要将其“包含”在测试可执行文件中。
编译目标文件gtest-all.o:
g++ -Igtest-1.6.0/include -Igtest-1.6.0 -c gtest-1.6.0/src/gtest-all.cc
然后构建库存档 libgtest.a:
ar -rv libgtest.a gtest-all.o
现在您可以在 ~/Downloads 中创建 test.cc 文件。这是一个示例测试文件,我用它来确保它可以编译。
#include "gtest/gtest.h"
TEST(blahTest, blah1) {
EXPECT_EQ(1, 1);
}
int main (int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
int returnValue;
//Do whatever setup here you will need for your tests here
//
//
returnValue = RUN_ALL_TESTS();
//Do Your teardown here if required
//
//
return returnValue;
}
要编译您自己的测试并运行它:
g++ -I/home/me/Downloads/gtest-1.6.0/include -pthread test.cc libgtest.a -o test_executable
然后执行它:
./test_executable
它应该运行良好。根据需要从那里进行修改。