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

/usr/bin/ld: cannot find -lgmock on ubuntu while running gmock program

本文关键字:ubuntu -lgmock 程序 bin usr ld gmock 运行      更新时间:2023-10-16

我正在尝试在我的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时,我会遇到此问题。

请帮助我解决。

让我知道更多信息。

查找taget_link_libraries的文档。检查FindGtest.cmake中的评论

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

您尚未完成gmock的find_package,因此没有针对gmock定义的变量。因为这不是标准的CMAKE模块,请自己写或从Internet中拿一张

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