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

3

7

ROS Fuerte I installed on my machine uses opencv 2.2. I would like to use 2.4.9 version just installed. Its location is /home/polar/soft/lib/opencv/opencv-2.4.9/build/lib.

How to do that with CMake, please? From my search, it seems that find_library would solve the issue, but was not able to make it work.

===== I include opencv in my cpp codes like this

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

=========== HERE MY 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)
Latitudinarian answered 13/2, 2015 at 19:26 Comment(12)
Have you tried find_package( OpenCV 2.9 ... )?Adage
find_package(OpenCV 2.4.9 PATHS /home/polar/soft/lib/opencv/opencv-2.4.9/cmake) does not work. Compilation error.Latitudinarian
1) You said you want to use version 2.9...? 2) "Compilation error"?Adage
Sorry, I need 2.4.9.Latitudinarian
"Compilation error"? Come on. You must realize that the information you're giving is in no way sufficient to give you any kind of assistance...Adage
I get undefined reference to cv::_InputArray::_InputArray(cv::Mat const&). The same error message related to the opencv functions am using.Latitudinarian
That is a linker error, not a compilation error. I see you have ${OpenCV_LIB} in there... but you only ever set that for corner_v1, and only if ( corners_opencv_flag ) -- which you set to ok. But if ( condition ) is true "if the constant is 1, ON, YES, TRUE, Y, or a non-zero number" -- note the absence of ok. And I'd check if ${OpenCV_LIB} is set as expected in the first place...Adage
the flag ok is only for the executables not the libraries. I suspect OpenCV_LIBLatitudinarian
Voting to close as "unclear what you're asking". Pal, my crystal ball is failing to make sense of the little tidbits you're willing to share. Either you accept that something is broken in your setup, and cooperate with people willing to invest their time into helping you (including double-checking your assumptions on what's working and what isn't), or you'll have to figure it out yourself.Adage
@devSolar I followed this link to install opencv [samontab.com/web/2014/06/… I will try to reinstall it from a different link and see what happen.Latitudinarian
P.S. am using ubmuntu 12.04 LTS.Latitudinarian
How is that supposed to help with your CMakeLists.txt apparently not adding the OpenCV libraries to whatever target you are trying to compile? If it hadn't found OpenCV, it would have failed at config stage due to the REQUIRED in find_package. Stop hunting what you think might be the error and go for proof. Check your assumptions. Or start actually answering the questions asked, instead of continuing an unrelated monologue.Adage
L
2

So, as I suspected, target_link_libraries(${file2link} .... ${OpenCV_LIB}) was the problem: OpenCV_LIB seems like allocated.

Now, am linking opencv in this way and it works:

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

In fact I just used another name but OpenCV.

@texasflood, thanks for your help.

Latitudinarian answered 16/2, 2015 at 14:54 Comment(2)
Is this a typo? OpenCVV_LIBMerriemerrielle
Yes, It is OpenCV_LIBSBraswell
M
7

Add this to your CMakeLists.txt replacing the previous find_package(OpenCV) line:

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

There should be a cmake directory in your opencv installation.

Missis answered 13/2, 2015 at 22:53 Comment(3)
The compiler still uses opencv from ROS, tough I commented the manigfest line including opencv.Latitudinarian
Can you post your full CMakeLists.txt?Missis
Worked for me (CMake 2.8.12.2 and OpenCV 3.2.0 on Ubuntu 12.4). I added it as an answer to stackoverflow.com/questions/37336667Moyer
L
2

So, as I suspected, target_link_libraries(${file2link} .... ${OpenCV_LIB}) was the problem: OpenCV_LIB seems like allocated.

Now, am linking opencv in this way and it works:

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

In fact I just used another name but OpenCV.

@texasflood, thanks for your help.

Latitudinarian answered 16/2, 2015 at 14:54 Comment(2)
Is this a typo? OpenCVV_LIBMerriemerrielle
Yes, It is OpenCV_LIBSBraswell
B
1

There is lot of confusion here and No Proper Code to solidify the suggestions.

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

Thank you.

Braswell answered 9/7, 2021 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.