sop*_*sop 8 c++ opencv googletest
我正在尝试使用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)'
Run Code Online (Sandbox Code Playgroud)
我正在写这样的测试:
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]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用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
)
Run Code Online (Sandbox Code Playgroud)
我之前已经做过这个,并且在一些提交之后它显示了这个错误.为什么它不再找到它?
Tom*_*dor 12
我在以下场景中得到了完整的错误消息:
使用凌乱的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.a,libgmock.a和libgmock_main.a),我发现该testing::internal::EqFailure功能需要2倍std::string,而不是testing::internal::String.!!
我检查了标题 - 没有任何地方 - std::string到处都是.奇怪的参考在哪里?
好吧 - 它是在旧的目标文件中,使用GTest 1.6标头构建!所以现在:
TL; DR
make clean或者那种效果,YMMV)小智 5
在这里,我提供了另一种可能是导致此类链接错误的原因:
在我的项目中,出于某些兼容性原因,我必须使用宏“-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,然后链接新库的方式解决了。
希望这个答案可以对某人有所帮助。
我和@yizhi-wang有同样的问题。它也与 @tomasz-gandor 和 @tim-rae 的问题非常相似:不同版本的字符串类型不同gtest,这通常是由std::stringGCC 4 和 5 之间发生的 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'
************************************************************************************
Run Code Online (Sandbox Code Playgroud)
我听从了建议,链接器错误消失了。