0
votes

I want to use GMock in my project. First of all I compiled GMock and GTest. The sequence of my actions (I use Linux):

  1. git clone googlemock and googletest
  2. go to GIT/googletest/googlemock/build-aux/ directory run cmake .. and then make
  3. As the result I got: libgmock.a and libgmock_main.a

My project has the structure: build, inc, src and lib directories. In build directory I run cmake .. and make. In inc dir I placed all headers from GIT/googletest/googlemock/include/ and GIT/googletest/googletest/include/. In lib dir lays libgmock.a only. In src - sources of my project.

My CMakeLists.txt contains:

project(blockchain)

cmake_minimum_required(VERSION 2.6)

set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB CPPS "*.cpp")

include_directories("../inc/")
link_directories("../lib/")

add_definitions(-Wall -O2 -std=c++11)
add_executable(${PROJECT_NAME} ${CPPS})
target_link_libraries(blockchain gmock)

install(TARGETS ${PROJECT_NAME} DESTINATION bin)

When I try to make my project (typing cmake .. && make in build dir) I get linking errors:

CMakeFiles/blockchain.dir/main.cpp.o: In function main': main.cpp:(.text.startup+0x13): undefined reference totesting::UnitTest::GetInstance()' main.cpp:(.text.startup+0x1b): undefined reference to testing::UnitTest::Run()' ../lib/libgmock.a(gmock-all.cc.o): In functiontesting::internal::scoped_ptr

::reset(std::__cxx11::basic_stringstream) [clone .part.81] [clone .constprop.367]': gmock-all.cc:(.text+0x21a): undefined reference to testing::internal::IsTrue(bool)' ../lib/libgmock.a(gmock-all.cc.o): In functiontesting::internal::MutexBase::AssertHeld() const [clone .constprop.368]': gmock-all.cc:(.text+0x274): undefined reference to testing::internal::IsTrue(bool)' gmock-all.cc:(.text+0x2b2): undefined reference totesting::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const, int)' gmock-all.cc:(.text+0x2fd): undefined reference to testing::internal::GTestLog::~GTestLog()' gmock-all.cc:(.text+0x312): undefined reference totesting::internal::GTestLog::~GTestLog()' ../lib/libgmock.a(gmock-all.cc.o): In function testing::internal::Expect(bool, char const*, int, std::__cxx11::basic_string, std::allocator > const&) [clone .part.51]': gmock-all.cc:(.text+0x910): undefined reference totesting::Message::Message()' gmock-all.cc:(.text+0x926): undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)' gmock-all.cc:(.text+0x931): undefined reference totesting::internal::AssertHelper::operator=(testing::Message const&) const' gmock-all.cc:(.text+0x939): undefined reference to testing::internal::AssertHelper::~AssertHelper()' gmock-all.cc:(.text+0x94b): undefined reference totesting::internal::IsTrue(bool)'

And a lot of others linking errors. What am I doing wrong?

1
Why do you move the headers and compiled libs from gtest/gmock directories to some directories in your project? According to gtest docs "robust and flexible approach is to build gtest as part of that project directly".pptaszni

1 Answers

1
votes

gmock depends on gtest. You added the former, but not the latter.