gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:13:44 42 4
gpt4 key购买 nike

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

在执行“make”时遇到错误

我有以下文件-

“测试.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);
};

“测试.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);
}

“模拟测试.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: cannot find -lgmock”问题会出现,即使我已经成功安装了 gmock。

我能够运行 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 : cannot find -lgmock on ubuntu while running gmock program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43798773/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com