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

How to force c++ compiler use one of different installed package's versions, using CMake?

本文关键字:安装 版本 强制 CMake 何使用 c++ 编译器      更新时间:2023-10-16

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("nn Boost found nn")
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)

将其添加到您的 CMakeList 中.txt替换前面的find_package(OpenCV)行:

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

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

所以,正如我所怀疑的那样,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,感谢您的帮助。

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

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

谢谢。