/ usr / bin / ld:在运行gmock程序时无法在ubuntu上找到-lgmock

时间:2017-05-05 07:15:29

标签: c++ ubuntu googletest googlemock

我正在尝试在我的ubuntu vmware(16.04 LTS)上编译简单的gmock示例

并在执行" make"

时低于错误

我有以下文件 -

" 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)

以下是我编译的步骤 -

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$

之后当我确实让我面对这个问题时

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$ 

我不知道为什么这个" / usr / bin / ld:找不到-lgmock"即使我已经成功安装了gmock,问题仍然存在。

我可以运行gtest程序,但是当我添加gmock时,我遇到了这个问题。

请帮我解决。

让我知道更多信息。

1 个答案:

答案 0 :(得分:1)

查找taget_link_libraries的文档。查看FindGtest.cmake

中的评论

您不应该使用-l指定库,而是使用find_package中的变量,例如: ${GTEST_LIBRARIES}

您还没有为GMOCK完成find_package,因此没有为GMOCK定义变量。由于这不是标准的CMake模块,请自己编写或从互联网上拿一个

但是,Google测试文档建议不要使用系统中已安装的库,而是在Project中自己构建它们。互联网上有几个例子如何将gtest / gmock作为ExternalProject添加到你的cmake项目中。