距离上次更新本文已经过去了 182 天,文章部分内容可能已经过时,请注意甄别

ubuntu 22.04.03 LTS 安装 google gtest 框架

1. 依赖项

首先在 ubuntu 中安装如下包

plaintext
1
sudo apt install -y unzip g++ gcc cmake make automake

2. 下载软件包

进入 google gtest 的 github 页面,下载源码包 Releases · google/googletest

plaintext
1
https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip

image.png

将其上传到 ubuntu 中,并解压。你也可以下载 tar.gz 的压缩包,并使用 tar -zxvf 命令解压。

plaintext
1
unzip googletest-1.14.0.zip

3. 安装

解压后进入文件夹

plaintext
1
cd googletest-1.14.0

依次执行如下命令,安装 google gtest;

plaintext
1
2
3
cmake .
sudo make
sudo make install

这样就安装成功了。

4. 测试是否正常安装

用如下测试代码看看是否能正常调用 gtest 模块。

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <gtest/gtest.h>
#include <iostream>

TEST(ADDTEST,ADDTEST_TRUE)
{
int num = 1;
EXPECT_EQ(num,1);
}


int main(int argc, char **argv) {
std::cout << "Running main() from gtest_main.cc\n";
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

编译并运行,没有问题!

plaintext
1
2
3
4
5
6
7
8
9
10
11
12
13
$ g++ test.cpp -o test -lgtest
$ ./test
Running main() from gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from ADDTEST
[ RUN ] ADDTEST.ADDTEST_TRUE
[ OK ] ADDTEST.ADDTEST_TRUE (0 ms)
[----------] 1 test from ADDTEST (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.

注意,gtest 是一个动态库,编译的时候需要加上 -lgtest 进行链接。