【问题标题】:OpenCV and Gtest conflict in CMakeCMake 中的 OpenCV 和 Gtest 冲突
【发布时间】:2014-03-18 17:30:50
【问题描述】:

我正在尝试对我的应用程序进行一些 Google 测试,但遇到了 OpenCVGTest 之间的一些冲突:

/usr/lib/gcc/i686-linux-gnu/4.6/../../../../lib/libgtest.a(gtest-all.cc.o): In function `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)':
gtest-all.cc:(.text+0xdd84): multiple definition of `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/local/lib/libopencv_ts.a(ts_gtest.cpp.o):ts_gtest.cpp:(.text._ZN7testing8internal8GTestLogC2ENS0_16GTestLogSeverityEPKci+0x0): first defined here 
...

GTest 用于opencv_ts 库。有谁知道如何解决这些多重定义?

我认为如果我只添加我从OpenCV 使用的库,它将得到解决,但我不知道该怎么做。我试过了:

target_link_libraries(${Exec8name}_test ${OpenCV}/opencv_core.so* ... )

target_link_libraries(${Exec8name}_test ${OpenCV_LIBS}/opencv_core.so* ... )

target_link_libraries(${Exec8name}_test ${OpenCV_LIBS_DIR}/opencv_core.so* ... )

等,但我得到的只是未找到或No rule to make tarket的错误

我试图删除两者之一,但我得到了

的错误
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
...

这是我的 CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

option(test "Build all tests." OFF)

set(EXECUTABLE_NAME MyProj)

project(${EXECUTABLE_NAME})

set(CMAKE_CXX_FLAGS "-g -Wall")

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

find_package(OpenCV REQUIRED)
find_package(Boost REQUIRED COMPONENTS filesystem 
                    system 
                    regex 
                    program_options)

add_executable(${EXECUTABLE_NAME}_Sol2 
              src/main/cpp/main.cpp

              src/main/cpp/solution2/MySol2.hpp
              src/main/cpp/solution2/MySol2.cpp
              )

target_link_libraries(${EXECUTABLE_NAME}_Sol2   ${OpenCV_LIBS}
                        ${Boost_LIBRARIES}
                        )

if (test)
  find_package(GTest REQUIRED)

  enable_testing()

  include_directories( ${GTEST_INCLUDE_DIRS} )

  add_executable(${EXECUTABLE_NAME}_Sol2_test 
            src/test/cpp/test_Sol2.cpp

            src/main/cpp/solution2/MySol2.hpp
            src/main/cpp/solution2/MySol2.cpp
            )


  target_link_libraries(${EXECUTABLE_NAME}_Sol2_test    ${OpenCV_LIBRARIES}
                            ${Boost_LIBRARIES}
                            )

  target_link_libraries(${EXECUTABLE_NAME}_Sol2_test    ${GTEST_LIBRARIES}
                            pthread
                            )

  add_test(${EXECUTABLE_NAME}_Sol2_test 
          ${CMAKE_CURRENT_BINARY_DIR}/${EXECUTABLE_NAME}_Sol2_test
          )

endif()

谁能告诉我一些解决方法?

【问题讨论】:

  • 您可以使用 cmake-gui 取消选择 OpenCV 的配置 BUILD_opencv_ts 并重建 OpenCV。然后再试一次。
  • 作为你的make错误,我认为你最好把你的cmake文件粘贴出来。
  • 我已经添加了cmake文件

标签: c++ linux opencv cmake googletest


【解决方案1】:

“opencv_ts”模块包含 gtest,因此您可以只包含所需的 OpenCV 模块,排除所有“ts”模块。例如:

find_package(OpenCV REQUIRED core imgproc highgui)

【讨论】:

  • 我认为你的答案更好,因为代码行数会更少。
【解决方案2】:

只添加我需要的库

target_link_libraries(${EXECUTABLE_NAME}_Sol2   opencv_core
                                                opencv_highgui
                                                opencv_imgproc
                                                ...
                       )

【讨论】:

    猜你喜欢
    • 2017-06-24
    • 2016-11-04
    • 2016-12-19
    • 2012-09-10
    • 2020-10-22
    • 2023-03-24
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多