簡體   English   中英

gtest 中對 testing::internal::EqFailure 的未定義引用

[英]undefined reference to testing::internal::EqFailure in gtest

我試圖做使用GoogleTest功能測試的現在是沒有找到了該EqFailure的事情

/usr/include/gtest/gtest.h:1337: undefined reference to `testing::internal::EqFailure(char const*, char const*, testing::internal::String const&, testing::internal::String const&, bool)'

我正在編寫這樣的測試:

test_file.cpp :

#include <gtest/gtest.h>

#include "tools/CMorphology.hpp"

TEST(erode_Morph, crossKernel_Morph)
{
  // initialize matrix to be eroded
  cv::Mat matrix = (cv::Mat_<uchar>(5, 5) << 1, 1, 1, 1, 1,
                                             1, 1, 0, 1, 1,
                                             1, 1, 1, 1, 1,
                                             1, 0, 1, 1, 1,
                                             1, 1, 1, 1, 1
            );
  // initialize the cross kernel
  cv::Mat kernel = cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 3));
  // initialize the vector expected as output
  cv::Mat verMat = (cv::Mat_<uchar>(5, 5) << 1, 1, 0, 1, 1,
                                             1, 0, 0, 0, 1,
                                             1, 0, 0, 1, 1,
                                             0, 0, 0, 1, 1,
                                             1, 0, 1, 1, 1);

  // call erode(...)
  Morphology morphology;
  cv::Mat matrixOut;
  morphology.erode(matrix, kernel, matrixOut);

  for (int i = 0; i < matrixOut.rows; i++)
  {
    for (int j = 0; j < matrixOut.rows; j++)
    {
      EXPECT_EQ(matrixOut.ptr<uchar>(i)[j], verMat.ptr<uchar>(i)[j]);
    }
  }
}

我正在使用 OpenCV,如果需要,我會發布其他文件。

CMake文件在這里:

cmake_minimum_required(VERSION 2.8)

set(EXECUTABLE_NAME lpdetect)
project(${EXECUTABLE_NAME})

option(DEBUG "Display images for each step" OFF)

if (DEBUG)
        set(CMAKE_CXX_FLAGS "-g -Wall -Wno-unknown-pragmas -Wno-reorder -Wno-sign-compare -Wno-switch -std=gnu++11 -DDISPLAY_IMGS -DBOOST_LOG_DYN_LINK")
else()
        set(CMAKE_CXX_FLAGS "-g -Wall -Wno-unknown-pragmas -Wno-reorder -Wno-sign-compare -Wno-switch -std=gnu++11 -DBOOST_LOG_DYN_LINK")
endif()

find_package(OpenCV REQUIRED core
                             imgproc
                             features2d
                             nonfree
                             highgui
                             )

find_package(Boost REQUIRED COMPONENTS filesystem
                                       program_options
                                       system
                                       thread
                                       locale
                                       regex
                                       date_time
                                       log
                                       log_setup
                                       timer
                                       )

include_directories(src/main/cpp
                    ${Boost_INCLUDE_DIRS}
                    ${OpenCV2_INCLUDE_DIRS}
                    )

add_executable( ${EXECUTABLE_NAME}
                             src/main/cpp/main.cpp
# and the other files
                             )

target_link_libraries(${EXECUTABLE_NAME} ${OpenCV_LIBS}
                                         "-laws-cpp"
                                         "-lcasablanca"
                                         ${Boost_LIBRARIES}
                                         "-lcrypto"
                                         )


find_package(GTest REQUIRED gtest_main
                            pthread)

enable_testing()

include_directories( ${GTEST_INCLUDE_DIRS} )

add_executable(${EXECUTABLE_NAME}_test src/test/cpp/test_Morphology.cpp
                                       src/main/cpp/tools/CMorphology.cpp
                                       src/main/cpp/tools/CMorphology.hpp
                       )

target_link_libraries(${EXECUTABLE_NAME}_test
                                       ${OpenCV_LIBRARIES}
                                       ${Boost_LIBRARIES}
                                       ${GTEST_LIBRARIES}
                                       )

add_test(${EXECUTABLE_NAME}_test
         ${EXECUTABLE_NAME}_test
         )

我很久以前就做過這個,在一些提交之后它顯示了這個錯誤。 為什么它不再找到那個?

在以下情況下,我確實收到了此錯誤消息:

使用凌亂的 Ubuntu 14.10( libgtest-dev (GTest 1.6) 和google-mock (GMock 1.7 與捆綁的 GTest 1.7)不匹配),我選擇了錯誤的路徑 - 安裝了 GMock 1.6,以匹配系統的libgtest-dev ) .

項目編譯了一段時間,但后來 - 在 git pull 之后,使用了 1.7 的一些新功能,我需要升級到 1.7。 我從 google-mock 包安裝它(用 CMake 重建,然后復制到/usr/include/usr/lib )。 然后,我啟動了構建,並且 gtest.h:1337(這個行號不是在說明嗎?)鏈接器錯誤開始發生。

在使用nm -C libgtest.a (重復libgtest_main.alibgmock.alibgmock_main.a )檢查庫數小時后,我發現testing::internal::EqFailure函數需要 2x std::string ,而不是testing::internal::String .!!

我檢查了標題 - 沒有任何東西 - std::string無處不在。 奇怪的參考在哪里?

好吧 - 它在舊的目標文件中,使用 GTest 1.6 頭文件構建! 所以現在:

TL; 博士

  • 刪除並重建您使用 GTest 1.6 標頭構建的所有舊對象。 make clean ,或者類似的東西,YMMV)
  • 升級 GMock,如果你還沒有。
  • 如果還沒有,請僅使用捆綁的 GTest(來自 GMock)。

與接受的解決方案相比,這可能不太可能成為問題,但在我的情況下,由於在我試圖編譯的項目上使用 g++-5 編譯 gtest 與 g++-4.8,我遇到了與 OP 相同的錯誤編譯,導致找不到符號

如果接受的答案對您不起作用,那么您可能應該仔細檢查您的項目是否使用了與 gtest 相同的編譯器。

在這里,我提供了另一種可能導致此類鏈接錯誤的情況:

在我的項目中,出於某些兼容性原因,我必須使用宏“-D_GLIBCXX_USE_CXX11_ABI=0”編譯我的項目,這會強制編譯器使用舊的 C++11 ABI 進行編譯(在 GCC>5.0 中)。 不過用的gtest庫是之前編譯過的,直接編譯就沒有這個選項了。

最后我得到了一個類似的錯誤,但 testing::internal::EqFailure 的函數簽名就像“testing::internal::EqFailure(char const*, char const*, std::__cxx11::string const&, std:: __cxx11::string const&, bool)”。 我不太記得確切的簽名,但字符串 args 中有類似“__cxx11”的東西,而不是 std::string。 當我發現它時,我發現這可能是由我使用的那個宏引起的。

所以這個問題終於通過用“-D_GLIBCXX_USE_CXX11_ABI=0”重新編譯gtest,然后鏈接新庫的方式解決了。

希望這個答案可以對某人有所幫助。

我的情況與第一個答案類似,我打算從源代碼構建 google 測試並將其鏈接到應用程序。 但是我在系統中安裝了 gtest-dev 包,並且忘記正確設置包含路徑。 刪除 gtest-dev 包並設置包含路徑后,問題就消失了。 Tomasz 的回答對縮小問題有很大幫助。

我和@yizhi-wang 遇到了同樣的問題。 它也與@tomasz-gandor 和@tim-rae 的問題非常相似:字符串類型在不同的gtest版本中有所不同,這通常是由發生在 GCC 4 和 5 之間的std::string ABI 中斷引起的。

就我而言,在發出conan install命令時,我注意到以下警告文本:

************************* WARNING: GCC OLD ABI COMPATIBILITY ***********************

Conan detected a GCC version > 5 but has adjusted the 'compiler.libcxx' setting to
'libstdc++' for backwards compatibility.
Your compiler is likely using the new CXX11 ABI by default (libstdc++11).

If you want Conan to use the new ABI, edit the default profile at:

    /home/john/.conan/profiles/default

adjusting 'compiler.libcxx=libstdc++11'

************************************************************************************

我遵循了建議,鏈接器錯誤消失了。

我的情況:對 ASSERT_STREQ(...) 的未定義聲明

解決方案:使用BUILD_GMOCK == OFF重建 gtest

對我有用,值得一試~

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM