【问题标题】:Entangled with Google test纠结谷歌测试
【发布时间】:2014-04-04 02:54:25
【问题描述】:

我无法启动和运行 Google 测试。我已经阅读了 Google 推荐的 steps,我还查看了以前的 post,并阅读了其他一些 examples,但并没有弄清楚很多事情。

为了简单起见,我正在尝试来自 Google 测试的建议示例,该示例可从 Android ndk 中的目录 - sample1 中获得:

// main.cpp

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "gtest/gtest.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    testing::InitGoogleTest(&argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/factorial/main.qml"));
    viewer.showExpanded();

    return RUN_ALL_TESTS();
}

// sample1_unittest.cpp

#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"

// Tests factorial of 0.
TEST(FactorialTest, Zero) {
  EXPECT_EQ(1, Factorial(0));
}

文件 sample1.h、sample1.cpp 也在项目中,其中包含阶乘函数。谷歌测试同样获知项目文件——factorial.pro:

INCLUDEPATH += 
/opt/android-studio/ndk/sources/third_party/googletest/googletest/include

当我按下 [Build > Build Project "factorial"] 时,会出现以下错误:

main.cpp:8: error: undefined reference to 'testing::InitGoogleTest(int*, char**)'
sample1_unittest.cpp:17: error: undefined reference to 'testing::Test::Test()'

我正在使用 Ubuntu、QtCreator、Android 和 C++。事实上,我已经花了 3 天的时间在周围嘲笑,但到目前为止没有得到太多。因此,我在这里发帖希望一些大师可以对此提供任何提示。任何帮助将不胜感激。

【问题讨论】:

  • 您是否已将 gtest.lib\gtest.a 添加到您的链接行?
  • @Wes - 刚刚做了:# find / -name gtest.a ... 没有任何结果。有什么建议应该在哪里,还是应该构建 gtest.a?谢谢!
  • 请注意,Google 建议您不要构建库,而是将 GTest 代码包含到您的项目中。见code.google.com/p/googletest/wiki/…

标签: android c++ unit-testing android-ndk qt-creator


【解决方案1】:

试试这样的:

$ g++ -I $GTEST_HOME/include -L $GTEST_HOME/lib -lgtest -lgtest_main -lpthread test.cpp

更多详情:

如果还是不行,可以考虑使用 Makefile:

# A sample Makefile for building Google Test and using it in user
# tests.  Please tweak it to suit your environment and project.  You
# may want to move it to your project's root directory.
#
# SYNOPSIS:
#
#   make [all]  - makes everything.
#   make TARGET - makes the given target.
#   make clean  - removes all files generated by make.

# Please tweak the following variable definitions as needed by your
# project, except GTEST_HEADERS, which you can use in your own targets
# but shouldn't modify.

# Points to the root of Google Test, relative to where this file is.
# Remember to tweak this if you move this file.
GTEST_DIR = ..

# Where to find user code.
USER_DIR = ../samples

# Flags passed to the preprocessor.
# Set Google Test's header directory as a system directory, such that
# the compiler doesn't generate warnings in Google Test headers.
CPPFLAGS += -isystem $(GTEST_DIR)/include

# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra -pthread

# All tests produced by this Makefile.  Remember to add new tests you
# created to the list.
TESTS = sample1_unittest

# All Google Test headers.  Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h

# House-keeping build targets.

all : $(TESTS)

clean :
    rm -f $(TESTS) gtest.a gtest_main.a *.o

# Builds gtest.a and gtest_main.a.

# Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized.  This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
gtest-all.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest_main.cc

gtest.a : gtest-all.o
    $(AR) $(ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
    $(AR) $(ARFLAGS) $@ $^

# Builds a sample test.  A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function.

sample1.o : $(USER_DIR)/sample1.cc $(USER_DIR)/sample1.h $(GTEST_HEADERS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1.cc

sample1_unittest.o : $(USER_DIR)/sample1_unittest.cc \
                     $(USER_DIR)/sample1.h $(GTEST_HEADERS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1_unittest.cc

sample1_unittest : sample1.o sample1_unittest.o gtest_main.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

如果您必须使用 Makefile 来使 gtest 正常工作,您可能需要针对您的情况调整给定的 template,因为您打算构建它以与 Android 一起使用。

【讨论】:

    【解决方案2】:

    您似乎没有根据您的描述构建 Google 测试。您需要将项目编译到库中,然后链接到它。如果您安装了CMake,那么您有两种选择:

    • 使用 CMake 的 GUI(相当直观)生成构建系统文件,然后照常使用这些文件(例如,如果您生成 Visual Studio 解决方案,打开 .sln 文件并构建项目)。
    • 使用命令行做同样的事情;本质上,您只需创建一个新目录并在其中执行cmake &lt;path-to-google-test&gt;。其余的都是一样的。

    您也可以自己构建库。该分发包含一个名为fused-src 的文件夹,该文件夹至少应包含两个文件:gtest_main.cppgtest-all.cpp。编译这些文件,你就完成了。你需要在这里生成两个库:gtest out of gtest-all.cpp and gtest_main out of gtest_main.cpp

    另一种选择是获取已经构建的库。我从来没有搜索过它们,但它们可能就在那里。

    【讨论】:

    • 不,我还没有构建,因为我读到它已经随 Android-NDK 一起提供了。我找了你提到的文件。我在 ./ndk/sources/third_party/googletest/googletest/src 文件夹中的 Android NDK 有以下文件:gtest_main.cc 和 gtest-all.cc,并且在同一目录中还有一个 gtest.cc。我没有安装 CMake,因为我正在使用 Qt Creator。我在 src 目录中没有看到任何使用 g++ 编译/构建它的 Make 文件。您是否建议至少使用 CMake 来构建 gtest.cc?谢谢!
    • @JessicaCohen 你必须将gtest-all.cc 编译成一个名为gtest 的库(你可以随意命名它,但你明白了),并将gtest_main.cc 编译成另一个名为gtest_main 的库.你可以在没有 CMake 的情况下做到这一点,尽管你可能会错过线程支持(这应该不是很多)。我没用过Android NDK自带的发行版,所以不知道有没有CMakeLists.txt文件。如果你不想弄乱 CMake,就照我说的简单做:将之前的文件编译成两个库并链接它们。
    猜你喜欢
    • 2012-03-30
    • 2012-08-17
    • 2011-04-02
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多