如何使用CMake强制c ++编译器使用不同安装包的版本之一?

时间:2015-02-13 19:26:08

标签: c++ opencv compilation cmake ros

我在我的机器上安装的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)

3 个答案:

答案 0 :(得分:6)

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

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

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

答案 1 :(得分: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,谢谢你的帮助。

答案 2 :(得分:0)

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

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

谢谢。