9

I am having trouble to get up and running with Google test. I have read the suggested steps from Google, I also looked a previous post, and read some other examples, but it doesn't clear much things up.

To keep things simple, I am trying the suggested example from Google test that is available from the directory in the 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));
}

The files sample1.h, sample1.cpp are also in the project, which contain the factorial function. Google test was equally informed to the project file - factorial.pro:

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

When I press [Build > Build Project "factorial"] it gets the following error:

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

I am working with Ubuntu, QtCreator, Android and C++. Indeed I have spent already 3 days mocking around, but getting not much anywhere so far. Thus, I am posting here in hope some guru may give any hint on this. Any help will be mostly appreciated.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jessica Cohen
  • 438
  • 3
  • 12
  • 2
    Have you added gtest.lib\gtest.a to your link line? – Wes Mar 03 '14 at 15:13
  • @Wes - Just did: # find / -name gtest.a ... and nothing comes out. Any suggestion where it is supposed to be, or should gtest.a be build? Thanks! – Jessica Cohen Mar 03 '14 at 22:39
  • Note that Google recommend that you DO NOT build a library, but instead include the GTest code into your project. See https://code.google.com/p/googletest/wiki/FAQ#Why_is_it_not_recommended_to_install_a_pre-compiled_copy_of_Goog – Mawg says reinstate Monica Aug 14 '15 at 14:21

2 Answers2

10

It seems you haven't built Google Test from what you describe. You need to compile the project into a library and then link against it. If you have CMake installed, then you have two options:

  • Use CMake's GUI (it's fairly intuitive) to generate the build system files, and then use those as usual (e.g. if you generate a Visual Studio solution, open the .sln file and build the project).
  • Use the command line to do the same thing; essentially you just create a new directory and do cmake <path-to-google-test> inside of it. The rest is the same.

You could also build the library by yourself. The distribution contains a folder named fused-src which should contain at least two files: gtest_main.cpp and gtest-all.cpp. Compile those files and you're done. You need to generate two libraries here: gtest out of gtest-all.cpp and gtest_main out of gtest_main.cpp.

Another alternative would be to get already built libraries. I've never searched for them, but they might be out there.

  • No, I haven't built yet, as I read that it already comes with the Android-NDK. I looked for the files you mentioned. What I have with the Android NDK in the folder ./ndk/sources/third_party/googletest/googletest/src are following files : gtest_main.cc and gtest-all.cc, and there is also a gtest.cc in the same directory. I don't have CMake installed as I am using Qt Creator. I don't see any Make file in the src directory to compile/build it using g++. Do you suggest using CMake at least for building the gtest.cc? Thanks! – Jessica Cohen Mar 05 '14 at 22:39
  • 1
    @JessicaCohen You have to compile `gtest-all.cc` into a library named `gtest` (you can name it whatever you want, but you get the idea), and `gtest_main.cc` into another named `gtest_main`. You can do this without CMake, although you might then miss threading support (which shouldn't be a great deal). I haven't used the distribution that comes with the Android NDK, so I don't know if it includes a CMakeLists.txt file. If you don't want to mess with CMake, simple do as I said: compile the previous files into two libraries and link against them. –  Mar 06 '14 at 14:31
5

Try something like this:

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

For more details:

If it still doesn't work, may find interesting to consider to use 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 $@

If you have to use Makefile to get gtest working, you probably may need to adjust the given template for your case, as you intend to build it to use with Android.

Community
  • 1
  • 1