【问题标题】:How to force c++ compiler use one of different installed package's versions, using CMake?如何使用 CMake 强制 c++ 编译器使用不同安装包的版本之一?
【发布时间】:2015-04-14 22:39:59
【问题描述】:

我机器上安装的ROS Fuerte使用的是opencv 2.2。我想使用刚安装的 2.4.9 版本。它的位置是/home/polar/soft/lib/opencv/opencv-2.4.9/build/lib

请问如何用 CMake 做到这一点? 根据我的搜索,find_library 似乎可以解决问题,但无法使其正常工作。

===== 我在这样的 cpp 代码中包含了 opencv

  #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/opencv.hpp>
    #include "opencv2/imgproc/imgproc.hpp"

===========这是我的 CMAKE

cmake_minimum_required(VERSION 2.8)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

rosbuild_init()
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

rosbuild_genmsg()
rosbuild_gensrv()


# GSL
find_package( PkgConfig REQUIRED)
pkg_check_modules( gsl REQUIRED gsl )

SET(corners_opencv_flag ok)

#*******************************************************************
#*******************************************************************

#******                CORNERS OPENCV

#*******************************************************************
#*******************************************************************
if(corners_opencv_flag)

  #---
  SET(execFiles_corner_opencv
    corner_v1

    )

  #---
  foreach(file_ros ${execFiles_corner_opencv})
    rosbuild_add_executable(${file_ros} computer-vision/corners/${file_ros}.cpp ) 
  endforeach(file_ros)

  #---
endif(corners_opencv_flag)


#-------------------
# STACK 
#--------------------

SET(FILES_TO_RUN
  ${execFiles_corner_opencv} 
  )


#=======================================================
#
#     CUSTOM LIBRARIES
#
#
#=======================================================
PROJECT(VOLCANO)
SET(SRC ${VOLCANO_SOURCE_DIR}/src)


#******* boost
find_package( Boost REQUIRED COMPONENTS program_options regex )
include_directories( ${Boost_INCLUDE_DIRS} )

if(Boost_FOUND)
  message("\n\n Boost found \n\n")
endif()

find_package(OpenCV REQUIRED PATHS /home/polar/soft/lib/opencv/opencv-2.4.9/cmake)


#===== Calculus
include_directories(${SRC}/calculus)
include_directories(${SRC}/calculus/matrix)
SET(MY_LIB
  ${MY_LIB}
 Calculus
 CholeskyDecompose
 )


#-------------------------------------------
# Linking the executables against the

#-------------------------------------------

foreach(file2link ${FILES_TO_RUN})

  target_link_libraries(${file2link} 
    ${MY_LIB} 
    ${MY_LIB} 
    ${gsl_LIBRARIES}
    ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_REGEX_LIBRARY}
    ${OpenCV_LIB}
   )

endforeach(file2link)  


#--- sources folders
ADD_SUBDIRECTORY(src)

【问题讨论】:

  • 你试过find_package( OpenCV 2.9 ... )吗?
  • find_package(OpenCV 2.4.9 PATHS /home/polar/soft/lib/opencv/opencv-2.4.9/cmake) 不起作用。编译错误。
  • 1) 您说要使用 2.9 版...? 2)“编译错误”?
  • 抱歉,我需要 2.4.9。
  • “编译错误”?快点。您必须意识到您提供的信息不足以为您提供任何帮助...

标签: c++ opencv compilation cmake ros


【解决方案1】:

将此添加到您的 CMakeLists.txt 中,替换之前的 find_package(OpenCV) 行:

find_package(OpenCV REQUIRED PATHS /home/polar/soft/lib/opencv/opencv-2.4.9/cmake)

你的opencv安装中应该有一个cmake目录。

【讨论】:

  • 编译器仍然使用来自 ROS 的 opencv,艰难我评论了包括 opencv 的清单行。
  • 你能发布完整的 CMakeLists.txt 吗?
  • 为我工作(Ubuntu 12.4 上的 CMake 2.8.12.2 和 OpenCV 3.2.0)。我将其添加为stackoverflow.com/questions/37336667 的答案
【解决方案2】:

所以,正如我所怀疑的,target_link_libraries(${file2link} .... ${OpenCV_LIB}) 是问题所在:OpenCV_LIB 似乎已分配。

现在,我正在以这种方式链接 opencv 并且它可以工作:

find_package(OpenCVV 2.4.9 PATHS /home/polar/soft/lib/opencv/opencv-2.4.9/cmake)
...
target_link_libraries(${file2link}  .... ${OpenCVV_LIB})

事实上我只是用了另一个名字,但OpenCV

@texasflood,感谢您的帮助。

【讨论】:

  • 这是错字吗? OpenCVV_LIB
  • 是的,是OpenCV_LIBS
【解决方案3】:

这里有很多混乱,没有正确的代码来巩固建议。

cmake_minimum_required(VERSION 3.1)

project( DisplayImage )

# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

# For OpenCV 4
SET(OpenCV_DIR <current-path>/Learning/opencv-installation/installation/OpenCV-master/lib/cmake/opencv4)

# For OpenCV 3
# SET(OpenCV_DIR "<current-path>/opencv3.4.8/installation/OpenCV-3.4.8/share/OpenCV/")

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

谢谢。

【讨论】:

    猜你喜欢
    • 2010-10-18
    • 2013-02-23
    • 2017-12-03
    • 1970-01-01
    • 2012-04-25
    • 2019-03-15
    • 2020-09-20
    • 2021-08-01
    • 1970-01-01
    相关资源
    最近更新 更多