如何编译gtest cpp文件? [英] How to compile a gtest cpp file?
问题描述
在 Ubuntu Linux 上,我一直遵循 gtest
给出的说明 此处 安装 gtest
,手动将头文件和库复制到 /usr/include
和 /usr/lib
>,分别.
On Ubuntu Linux, I have been following the gtest
instructions given here to install gtest
with manually copying the header files and libraries to /usr/include
and /usr/lib
, respectively.
然后我尝试编译以下代码(test1.cpp
)
I then tried to compile the following code (test1.cpp
)
#include <gtest/gtest.h>
TEST(MathTest, TwoPlusTwoEqualsFour) {
EXPECT_EQ(2 + 2, 4);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest( &argc, argv );
return RUN_ALL_TESTS();
}
使用以下命令
g++ -lgtest -lgtest_main -lpthread test1.cpp
只是为了看到另一个无用的错误消息:
just to see yet another unhelpful error message:
/usr/bin/ld: /tmp/ccQlmghI.o: undefined reference to symbol '_ZN7testing8internal9EqFailureEPKcS2_RKSsS4_b'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libgtest.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
我该如何解决这个问题?或者是否有另一个单元测试框架可以以简单的方式使用,并带有一个工作示例?
How can I fix this? Or is there another unittest framework which can be used in a SIMPLE way, with a working example?
在此处提出了同样的问题,但没有答案.
The same question has been asked here, but without an answer.
推荐答案
g++
的参数顺序很重要.源文件应该在目标文件和库(从高级到低级库)等之前.阅读关于 调用 GCC.
Order of arguments to g++
matters a lot. Source files should come before object files and libraries (from high-level to low-level libraries), etc. Read the chapter on invoking GCC.
那就试试吧:
g++ -Wall -g -pthread test1.cpp -lgtest_main -lgtest -lpthread
这篇关于如何编译gtest cpp文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!