2017-05-05 335 views
0

我試圖編譯在我的Ubuntu的VMware(16.04 LTS)在/ usr/bin中/ LD:無法在Ubuntu上找到-lgmock運行時gmock程序

簡單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) 

這些是我遵循編譯步驟 -

[email protected]:~/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 
[email protected]:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$ 

,之後當我的確讓我面對這個問題

[email protected]:~/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 
[email protected]:~/Desktop/for_gtest/ajay/gtest/ajay/demo/gmock/simple_demo$ 

我不知道爲什麼會這樣「的/ usr /斌/勞工處:找不到-lgmock」問題是,即使我已經安裝了gmock未來成功。

我能夠運行gtest程序,但是當我添加gmock時,我得到了這個問題。

請幫我解決。

讓我知道更多的信息。

+1

你能顯示用於鏈接編輯的確切行嗎(使VERBOSE = 1)? –

+0

哪一行?我想我已經添加了所有用於編譯代碼的文件和行。你能幫我理解你在找什麼,我錯過了嗎? – user2564083

回答

1

查找taget_link_libraries的文檔。檢查FindGtest.cmake中的註釋

您不應使用-l指定庫,而應使用find_package中的變量,例如${GTEST_LIBRARIES}

您還沒有爲GMOCK完成find_package,因此沒有爲GMOCK定義變量。由於這不是標準的CMake模塊,請自行編寫或從互聯網上下載一個

但是,Google測試文檔建議不要使用系統中已安裝的庫,而是在您的項目中自己構建它們。有幾個互聯網上的例子如何將gtest/gmock作爲ExternalProject添加到您的cmake項目中。

+0

我不明白你寫的是什麼。我對Google測試非常陌生。你能幫我理解更多嗎?或者請詳細寫下來幫助我。提前致謝 ! – user2564083

相關問題