undefined reference to testing::internal::EqFailure in gtest
Asked Answered
I

6

14

I am trying to do a test for a function using GoogleTest an now it is not finding anymore the EqFailure thing:

/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)'

I am writing the test like this:

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]);
    }
  }
}

I am using OpenCV and if needed, I'll post the other files.

CMake file is here:

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
         )

I have done this a wile ago, and after some commits it is displaying this error. Why does it no longer find that?

Impost answered 18/7, 2014 at 8:37 Comment(3)
Can you try running your build verbosely, and seeing if there is a -L link directory option to the directory containing your gtest libraries? Are you linking statically or dynamically? The error occur at link time (not runtime), correct?Sapodilla
It happens at link time, yes. I linked it dynamicallyImpost
Should it be Gtest_LIBRARIES instead of GTEST_LIBRARIES? I'm not too familiar with cmake, but the basic idea is that cmake should generate a -lgtest on the command line, and possible a -L/path/to/gtest. Can you see if that is part of your link command line? Possibly relevant: #8508223Sapodilla
W
19

I've had exactly this error message in the following scenario:

Using a messy Ubuntu 14.10 (which has a mismatch between libgtest-dev (GTest 1.6), and google-mock (GMock 1.7 with bundled GTest 1.7), I chose the wrong path - installed GMock 1.6, to match the system's libgtest-dev).

For some time the project compiled, but then - after a git pull, some new features of 1.7 were used and I needed to upgrade to 1.7. I installed it from google-mock package (rebuilt with CMake, then copied to /usr/include and /usr/lib). Then, I launched the build, and the gtest.h:1337 (isn't this line number telling?) linker error started happening.

After hours of inspecting the libs with nm -C libgtest.a (repeat for libgtest_main.a, libgmock.a and libgmock_main.a), I found that the testing::internal::EqFailure function takes 2x std::string, and not testing::internal::String.!!

I checked the headers - nothing there - std::string everywhere. Where was the strange reference?

Well - it was in the old object files, build with GTest 1.6 headers! So now:

TL;DR

  • Remove and rebuild all old objects which you built with GTest 1.6 headers. (make clean, or something to that effect, YMMV)
  • Upgrade GMock, if you haven't.
  • Use only bundled GTest (from GMock), if you haven't.
Whaleback answered 19/3, 2015 at 15:0 Comment(1)
Have this worked for you? Actually I have passed to boost test, I use boost, no? :)Impost
L
12

Here I provide another case that may possible be a reason for such a link error:

In my project, for some compatibility reason I have to compile my project with a macro "-D_GLIBCXX_USE_CXX11_ABI=0", which forces the compiler to use old C++11 ABI for compilation(in GCC>5.0). However, the gtest library used was compiled before, and was just compiled directly without this option.

And finally i got a similar error but the function signature of testing::internal::EqFailure is like "testing::internal::EqFailure(char const*, char const*, std::__cxx11::string const&, std::__cxx11::string const&, bool)". I don't quite remember the exact signature but there was something like "__cxx11" in the string args rather than std::string. And when I found it I figured out that this may caused by that macro I used.

So this problem was finally solved by recompiling the gtest with "-D_GLIBCXX_USE_CXX11_ABI=0" and then link the new library.

Hope this answer may help for someone.

Levite answered 1/1, 2019 at 12:20 Comment(0)
D
7

Probably this is less likely to be the problem compared with the accepted solution, but in my case I was getting the same error as the OP due to using g++-5 to compile gtest vs. g++-4.8 on the project which I was trying to compile, which resulted in symbols not being found.

If the accepted answer doesn't work for you then you should probably double check that you have used the same compiler for your project as gtest.

Dhammapada answered 10/5, 2016 at 12:32 Comment(2)
Also check you're using the same standard library as gtest (e.g. libstdc++ rather than libc++ depending on how you built gtest).Griner
That was it in my case. Used gcc 4.9 to build gtest and used gcc-8 to build my project. I went back to build gtest using "CC=gcc-8 CXX=/usr/bin/g++-8 cmake ../ && make && sudo make install" and it worked.Tooley
H
5

I had the same problem as @yizhi-wang. It's also very similar to @tomasz-gandor's and @tim-rae's issues: the string type differs across different gtest builds and this is often caused by a std::string ABI break which occured between GCC 4 and 5.

In my case, when issuing the conan install command, I noticed the following warning text:

************************* 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'

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

I followed the advice and the linker error went away.

Hulbert answered 31/8, 2019 at 23:35 Comment(2)
Also using Conan - updating the profile with conan profile update settings.compiler.libcxx=libstdc++11 default did the trick.Coprolite
Thank you very much. That was also my case and it fixed for me!Enjoin
S
3

My case was similar to the first answer, I intended to build google test from sources and link it to the application. But I had the gtest-dev package installed in the system, and forgot to set the include paths correctly. After removing the gtest-dev package and setting the include path the problem is gone. The answer from Tomasz helped a lot to narrow the issue.

Sandarac answered 15/4, 2016 at 9:30 Comment(2)
Worked for me too, just removed gtest-dev from the system.Decumbent
Same here, updating gmock and removing libgtest-dev helped.Grade
D
1

My case: undefined declaration to ASSERT_STREQ(...)

Solution: rebuild gtest with BUILD_GMOCK == OFF

It works for me, worth a try ~

Dissentious answered 10/4, 2020 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.