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

并在执行“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$

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

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

请帮我解决。

让我知道更多信息。

最佳答案

查找taget_link_libraries的文档。检查FindGtest.cmake中的注释

您不应使用-l指定它们的库,而应使用find_package中的变量,例如${GTEST_LIBRARIES}
您尚未为GMOCK完成find_package,因此没有为GMOCK定义任何变量。由于这不是标准的CMake模块,请自行编写或从Internet上获取一个

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

关于c++ -/usr/bin/ld:运行gmock程序时无法在ubuntu上找到-lgmock,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43798773/

10-11 00:44