Compiling using cmake with dependencies installed using macport

I am trying to create a project that is dependent on OpenCV. I installed Opencv using macports, and when I try to build a project, cmake gives the following result:

CMake Error at CMakeLists.txt:47 (FIND_PACKAGE): By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "OpenCV", but CMake did not find one. Could not find a package configuration file provided by "OpenCV" with any of the following names: OpenCVConfig.cmake opencv-config.cmake Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set "OpenCV_DIR" to a directory containing one of the above files. If "OpenCV" provides a separate development package or SDK, be sure it has been installed. 

I searched a bit for this problem and added the following env. variables in my $HOME/.profile file

 export DYLD_LIBRARY_PATH=/opt/local/lib:$DYLD_LIBRARY_PATH export CMAKE_PREFIX_PATH=/opt/local 

without success. I checked and all opencv files are listed in the / opt / local / lib and / opt / local / include / opencv directories. There is also OpenCVConfig.cmake along the following path:

/opt/local/lib/cmake/OpenCVConfig.cmake

How to make cmake find out the path where opencv is installed? Previously, I myself created OpenCV using cmake, and installed it in / usr / local, and then worked fine without any other fixes. However, I had some problems with ffmpeg, and now I'm switching to using macports.

+6
source share
7 answers

Another option that works for me was set to env OpenCV_DIR in the cmake opencv directory:

 export OpenCV_DIR=/opt/local/lib/cmake/ 
+8
source

macports traditionally installs OpenCV on /opt/local/ instead of the standard /usr/local/ .

The solution to your problem is indicated at:

Add the installation prefix "OpenCV" to CMAKE_PREFIX_PATH or set "OpenCV_DIR" to the directory containing one of the above files.

When creating a project on the command line, make sure that you run:

 export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/local/lib/pkgconfig export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/local/lib 

Then set the CMAKE_PREFIX_PATH flag for cmake:

 cmake -D CMAKE_PREFIX_PATH=/opt/local ../ 
+1
source

Finally, I read the header of the OpenCVConfig.cmake file. He instructs to include these lines for use from an external project:

 find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) target_link_libraries(MY_TARGET_NAME ${OpenCV_LIBS}) 

(adding include_directories to CMakeLists.txt fixed for me)

+1
source

Not a MACPORT problem, but someone might find this helpful. Subsequent @ hugh-pearse and @ leszek-hanusz answer this question with a little tweaking. I installed opencv from the ubuntu 12.10 repository (libopencv -) * and had the same problem. This could not be solved using export OpenCV_DIR=/usr/share/OpenCV/ (from my OpenCVConfig.cmake, which is there). It was resolved when I changed some lines in the OpenCVConfig.cmake file:

 # ====================================================== # Include directories to add to the user project: # ====================================================== # Provide the include directories to the caller #SET(OpenCV_INCLUDE_DIRS "${OpenCV_INSTALL_PATH}/include/opencv;${OpenCV_INSTALL_PATH}/include") SET(OpenCV_INCLUDE_DIRS "/usr/include/opencv;/usr/include/opencv2") INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS}) # ====================================================== # Link directories to add to the user project: # ====================================================== # Provide the libs directory anyway, it may be needed in some cases. #SET(OpenCV_LIB_DIR "${OpenCV_INSTALL_PATH}/lib") SET(OpenCV_LIB_DIR "/usr/lib") LINK_DIRECTORIES(${OpenCV_LIB_DIR}) 

And it worked on my Ubuntu 12.10. Remember to add target_link_libraries(yourprojectname ${OpenCV_LIBS}) to your CMakeLists.txt.

0
source

I tried all of the above ideas in vain. I eventually found a way to work with compilation: in addition to using the @kengregson steps, I simply renamed the / usr / local / include / opencv 2 folder so that it would not be selected when compiling my cpp file.

0
source

Since I am compiling my own OpenCV on a multi-user server, so I cannot directly install the OpenCV libraries in / usr / local, but the same problem occurs in my home folder instead.

The following describes how I fix it:

cmake_minimum_required(VERSION 2.8) project( DisplayImage ) find_package( OpenCV) add_executable( DisplayImage DisplayImage.cpp ) target_link_libraries( DisplayImage ${OpenCV_LIBS} )

  1. The same error occurred, so add one environment variable to my .tcshrc

setenv OpenCV_DIR "${folder where contains OpenCVConfig.cmake }" \# mine is: setenv OpenCV_DIR "~/local/OpenCV2.4.13/share/OpenCV"

0
source

Make sure you compile OpenCV as soon as you compile OpenCVConfig.cmake will be generated in the assembly directory.

follow these steps to compile here

then export OpenCV_DIR=<path to build directory with OpenCVConfig.cmake>

It should work now!

0
source

Source: https://habr.com/ru/post/927963/


All Articles