如何使用 CMake 强制 C++ 编译器使用不同安装包的版本之一?
How to force c++ compiler use one of different installed package's versions, using CMake?
我机器上安装的ROS富尔特使用的是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_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)
将此添加到您的 CMakeLists.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} )
谢谢。
我机器上安装的ROS富尔特使用的是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_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)
将此添加到您的 CMakeLists.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} )
谢谢。