2

I am trying to compile simple gmock example on my ubuntu vmware(16.04 LTS)

and getting below error while doing "make"

I have below files -

"test.h"

class CBasicMath
{
public:
    CBasicMath(){}
    virtual ~CBasicMath() {}
    virtual int Addition(int x, int y);
    virtual int Multiply(int x, int y);
    virtual int Divide(int x, int y);
};

"test.cpp"

#include "test.h"

int CBasicMath::Addition(int x, int y)
{
   return (x + y);
}

int CBasicMath::Multiply(int x, int y)
{
   return (x * y);
}

int CBasicMath::Divide(int x, int y)
{
   return (x / y);
}

"mocktest.h"

#include "gmock/gmock.h"
 #include "test.cpp"

class MockBasicTest : public CBasicMath {
public:
    MOCK_METHOD2(Addition, int(int x, int y));
    MOCK_METHOD2(Multiply, int(int x, int y));
    MOCK_METHOD2(Divide, int(int x, int y));
};

"main.cpp"

#include "mocktest.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"

TEST(BasicMathTest, testAddition) {
    MockBasicTest basictest;
    EXPECT_CALL(basictest, Addition(2,3)).Times(0);

//    EXPECT_EQ(0, basictest.Addition(2,3));
/*
        .Times(5);
    EXPECT_EQ(0,basictest.Addition(2,3));
    EXPECT_EQ(0,basictest.Addition(2,3));
    EXPECT_EQ(0,basictest.Addition(2,3));
    EXPECT_EQ(0,basictest.Addition(2,3));
    EXPECT_EQ(0,basictest.Addition(2,3));
*/
}


int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

"CMakeLists.txt"

cmake_minimum_required(VERSION 2.6)

    # Locate GTest
    find_package(GTest REQUIRED)
    include_directories(${GTEST_INCLUDE_DIRS})

    # Link runTests with what we want to test and the GTest and pthread library
    add_executable(runTests main.cpp)
    target_link_libraries(runTests -lgtest -lgmock -lpthread)

These are the steps I followed for compilation -

ajay@ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$ cmake CMakeLists.txt 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ajay/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo
ajay@ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$

and after that When I did make I am facing the issue

ajay@ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$ make
Scanning dependencies of target runTests
make[2]: Warning: File 'main.cpp' has modification time 84978 s in the future
[ 50%] Building CXX object CMakeFiles/runTests.dir/main.cpp.o
[100%] Linking CXX executable runTests
/usr/bin/ld: cannot find -lgmock
collect2: error: ld returned 1 exit status
CMakeFiles/runTests.dir/build.make:94: recipe for target 'runTests' failed
make[2]: *** [runTests] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
ajay@ubuntu:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$ 

I don't know why this "/usr/bin/ld: cannot find -lgmock" issue is coming even though I have installed gmock successfully.

I am able to run gtest programs but when I am adding gmock I am getting this issue.

Please help me to resolve.

Let me know for more info.

3
  • 1
    Can you show the exact line used for link editing (make VERBOSE=1)? Commented May 5, 2017 at 8:47
  • Which line ? I guess I have added all the files and lines used to compile the code. Can you please help me to understand what that you are looking and I missed Commented May 8, 2017 at 4:54
  • I'm also stuck on this. I'm new to cmake, I tried literally everything. Configure linker, added LD_LIBRARY_PATH etc.. nothing worked. Did you solved this? Commented Apr 14, 2021 at 13:18

1 Answer 1

1

Lookup the documentation for taget_link_libraries. Check the comments in FindGtest.cmake

You should not specify libraries them with -l instead use the variables from find_package e.g. ${GTEST_LIBRARIES}

You haven't done find_package for GMOCK so there are no variables defined for GMOCK. As this is not a standard CMake module, write your own or take one from the Internet

BUT, the Google test documentation recommends not use the installed libraries from the system, but to build them yourself inside your Project.There are several examples on the internet how to add gtest/gmock as ExternalProject to your cmake project.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't understand what you wrote. I am very new to google test. Can you please help me to understand more or please write in detail and help me out. Thanks in advance !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.