我正在学习谷歌测试。 我下载了gtest,运行了命令./configure并make并结束了
$ sudo cp -a include/gtest /usr/include $ sudo cp -a lib/.libs/* /usr/lib/我从这里得到了这一切。 我试着运行这段代码
#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(); }喜欢这个
$ export GTEST_HOME=~/usr/gtest $ export LD_LIBRARY_PATH=$GTEST_HOME/lib:$LD_LIBRARY_PATH $ g++ -I $GTEST_HOME/include -L $GTEST_HOME/lib -lgtest -lgtest_main -lpthread test.cpp但是我收到了一个错误
/usr/bin/ld: /tmp/ccVTj3Rk.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我做错了什么或者它是一个错误?
I'm learning to google test. I downloaded gtest, ran commands ./configure and make and ended with
$ sudo cp -a include/gtest /usr/include $ sudo cp -a lib/.libs/* /usr/lib/i got all this from here. I tried to run this code
#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(); }like this
$ export GTEST_HOME=~/usr/gtest $ export LD_LIBRARY_PATH=$GTEST_HOME/lib:$LD_LIBRARY_PATH $ g++ -I $GTEST_HOME/include -L $GTEST_HOME/lib -lgtest -lgtest_main -lpthread test.cppbut i received an error
/usr/bin/ld: /tmp/ccVTj3Rk.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 statusAm i doing something wrong or is it a bug?
最满意答案
您的设置中存在一些错误。
您将include / gtest复制到/ usr / include ( sudo cp -a include/gtest /usr/include ),但是当您尝试编译时,告诉编译器在〜/ usr / gtest目录中查找gtest头,不在你之前设置的目录中( / usr / include / gtest )。 使用lib / .libs / *文件发生了同样的事情。 你在一个地方复制它们,在编译时你在另一个地方要求它们。
保持以下配置:
$ sudo cp -a include/gtest /usr/include $ sudo cp -a lib/.libs/* /usr/lib/将#include <gtest/gtest.h>更改为#include "gtest/gtest.h"并尝试编译如下:
g++ -g -Wall <your .cpp files> -I /usr/include/gtest/ -L /usr/lib/ -lgtest -lgtest_main -lpthread在运行./a.out文件(或您为可执行文件指定的任何名称)后,您应该看到以下输出
[==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from MathTest [ RUN ] MathTest.TwoPlusTwoEqualsFour [ OK ] MathTest.TwoPlusTwoEqualsFour (0 ms) [----------] 1 test from MathTest (0 ms total) [----------] Global test environment tear-down [==========] 1 test from 1 test case ran. (0 ms total) [ PASSED ] 1 test.There are some errors in your setup.
You copied your include/gtest into /usr/include (sudo cp -a include/gtest /usr/include), but when you try to compile you tell the compiler to look for the gtest headers in the ~/usr/gtest directory, not in the directory you set up before (/usr/include/gtest). The same things happed with the lib/.libs/* files. You copy them in a place and at compile time you ask for them in another place.
Keeping the following configurations:
$ sudo cp -a include/gtest /usr/include $ sudo cp -a lib/.libs/* /usr/lib/change #include <gtest/gtest.h> into #include "gtest/gtest.h" and try to compile like this:
g++ -g -Wall <your .cpp files> -I /usr/include/gtest/ -L /usr/lib/ -lgtest -lgtest_main -lpthreadAnd you should see the following output after running the ./a.out file (or whatever name you assign to the executable file)
[==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from MathTest [ RUN ] MathTest.TwoPlusTwoEqualsFour [ OK ] MathTest.TwoPlusTwoEqualsFour (0 ms) [----------] 1 test from MathTest (0 ms total) [----------] Global test environment tear-down [==========] 1 test from 1 test case ran. (0 ms total) [ PASSED ] 1 test.更多推荐
发布评论