Compiling using cmake with dependencies installed using macport - opencv

I'm trying to build project that has dependency to OpenCV. I installed Opencv using macports and when I try to build project, cmake gives the following output:
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 little bit about this problem and added the following env. variables to 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 I have all opencv files istalled in /opt/local/lib and /opt/local/include/opencv directories. There is also OpenCVConfig.cmake in the following path:
/opt/local/lib/cmake/OpenCVConfig.cmake
How to make cmake know the path where opencv is installed? Previously I've build OpenCV on my own using cmake and installed into /usr/local and then it worked fine without any other fix. However I had some problems with ffmpeg and right now I switched to using macports.

Another option that works for me was set the env value OpenCV_DIR at the cmake opencv dir:
export OpenCV_DIR=/opt/local/lib/cmake/

macports traditionally installs OpenCV to /opt/local/ instead of the standard /usr/local/.
The solution to your problem is stated at:
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files.
When building your project in the command-line, make sure you execute:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/local/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/local/lib
And then set the flag CMAKE_PREFIX_PATH for cmake:
cmake -D CMAKE_PREFIX_PATH=/opt/local ../

Not a MACPORT problem, but someone may find this helpful. Followed #hugh-pearse 's and #leszek-hanusz 's answers in this question, with a little tweak. I had installed opencv from ubuntu 12.10 repository (libopencv-)* and had the same problem. Couldn't solve it with export OpenCV_DIR=/usr/share/OpenCV/ (since my OpenCVConfig.cmake whas there). It was solved when I changed some lines on 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 that worked on my Ubuntu 12.10. Remember to add the target_link_libraries(yourprojectname ${OpenCV_LIBS}) in your CMakeLists.txt.

I finally read the header of the OpenCVConfig.cmake file.
It instructs to include these lines to 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 it for me)

Make sure you have compiled OpenCV, once you compiled, OpenCVConfig.cmake will be generated in build directory.
follow these steps to compile
then , export OpenCV_DIR=<path to build directory with OpenCVConfig.cmake>
It should work now !

I tried all the above ideas in vain.
I eventually found a way to have the compilation work: Besides using #kengregson steps, I simply renamed the folder /usr/local/include/opencv2 so that it is not chosen when compiling my cpp file.

Since I compile my own OpenCV in a multiple-users server, so I can't install the OpenCV libs in /usr/local directly, but in my home folder instead, then exactly the same issue occurs to me.
Below is how I fix it:
Flowing the steps in OpenCV official doc: Using OpenCV with gcc and CMake
Add displayImage test program, and use this CMake file
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV)
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
The same error happened, so add one environment variable in my .tcshrc
setenv OpenCV_DIR "${folder where contains OpenCVConfig.cmake }"
\# mine is:
setenv OpenCV_DIR "~/local/OpenCV2.4.13/share/OpenCV"

Related

Trying to build OpenSfm but my OpenCv build doesn't have the proper Cmake file

I tried running 'python setup.py build' to build Open SfM. I had used the instructions off the OpenCV website to build OpenCV using gitbash and Cmake in my lib folde without any errors.
However the OpenSfM build error says: Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
I can't for the life of me find these files. I tried downloading the OpenCv package again and running it through a Cmake GUI but I get an error saying there's no CmakeLists.txt files.
Help?
You need to not just generate the files using CMake but also use either an application like VisualStudio or command line to build the files.
Once you build the files, the OpenCVConfig.cmake files are generated, which you use to point to the OpenSFM compilation.

How do i include opencv into my catkin_workspace?

I am currently having issues with the find_package(opencv) which give me the error message:
-- Could not find the required component 'OpenCV'. The following CMake error indicates that you either need to install the package with the same name or change your environment so that it can be found.
CMake Error at /opt/ros/indigo/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
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.
So.. Yeah, a solution would add the prefix_path, but how do i make it work with find_package() without explicitly stating the path it has to look for.. why can't it find it in the first place, the problem by doing this is that the CMakelist is shared amongst a group a people, in which I am the only one having this issue despite, having the .cmake file at the same location..
I am running it on a ubuntu machine.
OpenCV v2.4.8
And the .cmake files are located at
/usr/local/share/OpenCV/OpenCVConfig.cmake
/usr/share/OpenCV/OpenCVConfig.cmake
opencv is installed as sudo apt-get install libopencv-dev

Gtest_main _dir not found while installing libqi for ros-nao

I am trying to install libqi for nao-ros and it gives me error: I already have gtest package but gtest_main is missing.
CMake Error at /home/shruti/d-prefix/share/cmake/qibuild/internal/uselib.cmake:80 (find_package): By not providing "FindGTEST_MAIN.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "GTEST_MAIN", but CMake did not find one.
Could not find a package configuration file provided by "GTEST_MAIN" with any of the following names:
GTEST_MAINConfig.cmake
gtest_main-config.cmake
Add the installation prefix of "GTEST_MAIN" to CMAKE_PREFIX_PATH or set "GTEST_MAIN_DIR" to a directory containing one of the above files. If "GTEST_MAIN" provides a separate development package or SDK, be sure it has been installed.
Any one knows about it?
I had the same problem when trying to build libqi and fixed it by:
cd /usr/share/cmake-3.0/Modules
ln -s FindGTest.cmake FindGTEST_MAIN.cmake

Build errors while following the instructions given on a website

Hi i've tried following the instructions from this website http://robotica.unileon.es/mediawiki/index.php/Objects_recognition_and_position_calculation_(webcam)
At the part where they asked to add this :
find_package(OpenCV "VERSION" REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
...
target_link_libraries("PROGRAM_NAME" ${OpenCV_LIBS})
i added and tried to build the package but it resulted in the errors:
Parse error. Expected a command name, got unquoted argument with text "...".
-- Configuring incomplete, errors occurred!
make: *** [cmake_check_build_system] Error 1
Invoking "make cmake_check_build_system" failed
Could any one please help me resolve this error?
This basically describes how you're able to add OpenCV to your own program, if you're actually using CMake.
The text in quotes has to be replaced with your actual values/project names, for example, if my target is called myopencvthing and I'd want to use OpenCV 2.0 or newer, then I'd setup something like this:
# First tell CMake the minimal version of CMake expected
cmake_minimum_required(VERSION 2.8)
# Define a project
project(myopencvproject)
# Tell CMake to look for OpenCV 2.0 (and tell it that it's required, not optional)
find_package(OpenCV 2.0 REQUIRED)
# Tell CMake to add the OpenCV include directory for the preprocessor
include_directories(${OpenCV_INCLUDE_DIRS})
# Add the source files to a variable
set(SOURCES main.cpp processing.cpp somethingelse.cpp)
# Define the actual executable target and the source files
add_executable(myopencvthing ${SOURCES})
# Finally, add the dependencies of our executable (i.e. OpenCV):
target_link_libraries(myopencvthing ${OpenCV_LIBS})
Now you'll just have to run CMake to create your actual makefiles or project files and then build everything, e.g.:
cd /my/build/dir
cmake /path/to/my/source
make
If CMake fails to find the specified dependencies, then you'll have to open the CMakeCache.txt file and edit those paths by hand (or use cmake-gui in case you prefer a more visual editor).
I think you should replace "VERSION" and "PROGRAM_NAME" with the Version number and the program name.
This is
2.4 for VERSION
and
the program name by whatever you named your executable.

Could not find module FindOpenCV.cmake ( Error in configuration process)

I wrote a CMakeLists.txt for a project in C++, which uses OpenCV libraries. When I try to create the project using cmake, I get the next configuration problem:
CMake Error at CMakeLists.txt:15 (find_package):
Could not find module FindOpenCV.cmake or a configuration file for package
OpenCV.
Adjust CMAKE_MODULE_PATH to find FindOpenCV.cmake or set OpenCV_DIR to the
directory containing a CMake configuration file for OpenCV. The file will
have one of the following names:
OpenCVConfig.cmake
opencv-config.cmake
The fact is that I have an environment variable for the path which I use in Visual Studio with no problems. If I don't include OpenCV, then I can configure and generate with no problem, but I need to solve the problem. I don't understand why cmake cannot find the OpenCV path or how to fix it.
I also used the recommendations mentioned in this link:
FindOpenCV.cmake
Does anybody had this problem too?
The error you're seeing is that CMake cannot find a FindOpenCV.cmake file, because cmake doesn't include one out of the box. Therefore you need to find one and put it where cmake can find it:
You can find a good start here. If you're feeling adventurous you can also write your own.
Then add it somewhere in your project and adjust CMAKE_MODULE_PATH so that cmake can find it.
e.g., if you have
CMakeLists.txt
cmake-modules/FindOpenCV.cmake
Then you should do a
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)
In your CMakeLists.txt file before you do a find_package(OpenCV)
If you are on Linux, you just need to fill the OpenCV_DIR variable with the path of opencv (containing the OpenCVConfig.cmake file)
export OpenCV_DIR=<path_of_opencv>
apt-get install libopencv-dev
export OpenCV_DIR=/usr/share/OpenCV
the header of cpp file should contain:
#include
#include "opencv2/highgui/highgui.hpp"
#include
#include
not original cv.h
find / -name "OpenCVConfig.cmake"
export OpenCV_DIR=/path/found/above
I had this exact same problem. I fixed it by adding the following line to my FindOpenCV.cmake file. Put it anywhere at the top before the rest of the code.
set (OpenCV_DIR /home/cmake/opencv/compiled) #change the path to match your complied directory of opencv
Basically you are telling FindOpenCV.cmake where to find opencv files assuming the other compilation can find the FindOpenCV.cmake
I faced the same error. In my case this "OpenCVConfig.cmake" file is located in /usr/local/share/OpenCV. In CMakeLists.txt add the line
set(OpenCV_DIR /usr/local/share/OpenCV)
as suggested by the error message.
if you are on windows, you can add opencv path to OpenCV_DIR yourself.
(OpenCV_DIR is in the red region)
the path is like "D:/opencv244/build".
you can find file "OpenCVConfig.cmake" under the path.
Another possibility is to denote where you can find OpenCV_DIR in the CMakeLists.txt file. For example, the following cmake scripts work for me:
cmake_minimum_required(VERSION 2.8)
project(performance_test)
set(OpenCV_STATIC ON)
set(OpenCV_CUDA OFF)
set(OpenCV_DIR "${CMAKE_SOURCE_DIR}/../install")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIR})
file(GLOB my_source_files ./src/*)
add_executable( performance_test ${my_source_files})
target_link_libraries(performance_test ${OpenCV_LIBS})
Just to remind that you should set OpenCV_STATIC and OpenCV_CUDA as well before you invoke OpenCVConfig.cmake. In my case the built library is static library that does not use CUDA.
On my Fedora machine, when I typed "make" I got an error saying it could not find "cv.h". I fixed this by modifying my "OpenCVConfig.cmake" file.
Before:
SET(OpenCV_INCLUDE_DIRS "${OpenCV_INSTALL_PATH}/include/opencv;${OpenCV_INSTALL_PATH}/include")
SET(OpenCV_LIB_DIR "${OpenCV_INSTALL_PATH}/lib64")
After:
SET(OpenCV_INCLUDE_DIRS "/usr/include/opencv;/usr/include/opencv2")
SET(OpenCV_LIB_DIR "/usr/lib64")
I am using Windows and get the same error message. I find another problem which is relevant.
I defined OpenCV_DIR in my path at the end of the line. However when I typed "path" in the command line, my OpenCV_DIR was not shown. I found because Windows probably has a limit on how long the path can be, it cut my OpenCV_DIR to be only part of what I defined. So I removed some other part of the path, now it works.
I had the same error, I use windows. I add "C:\opencv\build" (opencv folder) to path at the control pannel.
So, That's Ok!!
For me (on Ubuntu), I just run:
sudo apt-get install libopencv-dev
Followed #hugh-pearse 's and #leszek-hanusz 's answers, with a little tweak. I had installed opencv from ubuntu 12.10 repository (libopencv-)* and had the same problem. Couldn't solve it with export OpenCV_DIR=/usr/share/OpenCV/ (since my OpenCVConfig.cmake whas there). It was solved when I also changed some lines on 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 that worked on my Ubuntu 12.10. Remember to add the target_link_libraries(yourprojectname ${OpenCV_LIBS}) in your CMakeLists.txt.
When you install the libraries in the c drive (windows). the CMakeLists.txt shoud be looking like below:
cmake_minimum_required(VERSION 3.0.0)
project(test_opencv VERSION 0.1.0)
include(CTest)
enable_testing()
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(test_opencv main.cpp)
target_link_libraries(test_opencv ${OPENCV_LIBS})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
finding the package and include directories
when building the project in VS code. Run the visual studio code with admin rights as the OpenCV is installed inside C drive.

Resources