我使用muon搜索googletests,但是看起来ubuntu没有用于它的包。我需要使用源代码安装吗?
发布于 2012-01-22 17:59:35
新资料:
值得注意的是,libgtest0已不复存在。截至2013年左右(我不确定更改日期),见以下问题:
2012年以前的旧答案:
它在Ubuntu存储库中。
sudo apt-get install libgtest0 libgtest-dev另见man gtest-config
发布于 2020-11-25 13:37:43
由于Debian/Ubuntu拒绝打包预构建(如:为什么没有安装用于google测试的库文件?中提到的那样),我将自己克隆并构建它(或者在实际项目中,将其添加为子模块):
git clone https://github.com/google/googletest
cd googletest
git checkout b1fbd33c06cdb0024c67733c6fdec2009d17b384
mkdir build
cd build
cmake ..
make -j`nproc`
cd ../..然后,我将它与测试文件main.cpp一起使用:
g++ \
-Wall \
-Werror \
-Wextra \
-pedantic \
-O0 \
-I googletest/googletest/include \
-std=c++11 \
-o main.out \
main.cpp \
googletest/build/lib/libgtest.a \
-lpthread \
;main.cpp
#include <gtest/gtest.h>
int myfunc(int n) {
return n + 1;
}
TEST(asdfTest, HandlesPositiveInput) {
EXPECT_EQ(myfunc(1), 2);
EXPECT_EQ(myfunc(2), 3);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}要获得预期的输出:
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from asdfTest
[ RUN ] asdfTest.HandlesPositiveInput
[ OK ] asdfTest.HandlesPositiveInput (0 ms)
[----------] 1 test from asdfTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.或者,您也可以从main文件中移除main.cpp函数,取而代之的是使用由libgtest_main.a:
g++ \
-Wall \
-Werror \
-Wextra \
-pedantic \
-O0 \
-I googletest/googletest/include \
-std=c++11 \
-o main.out \
main.cpp \
googletest/build/lib/libgtest.a \
googletest/build/lib/libgtest_main.a \
-lpthread \
;在Ubuntu 20.04上测试。
https://askubuntu.com/questions/97626
复制相似问题