CMake is having problems to compile a project which uses OpenCV - c++

I'm on Linux Mint 19.1 Tessa.
I followed the instructions described here to install OpenCV. Now I have it installed at the directory "/home/dell/opencv".
I tryed to run the example project located at "/home/dell/opencv/samples/cpp/example_cmake/" by running the command "cmake ." on terminal and I got the following error:
CMake Error at CMakeLists.txt:14 (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.
-- Configuring incomplete, errors occurred!
See also "/home/dell/opencv/samples/cpp/example_cmake/CMakeFiles/CMakeOutput.log".
The "CMakeLists.txt" file contains the following:
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Define project name
project(opencv_example_project)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# 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 " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)
# Link your application with OpenCV libraries
target_link_libraries(opencv_example LINK_PRIVATE ${OpenCV_LIBS})
I've searched a lot through the internet and it seems to be a common problem. However, I still didn't manage to solve it by following the intructions that I've found.
One thing that I notice when going through the OpenCV folder is that the version that I have (which I think it's the most recent one) indeed doesn't contain any "OpenCVConfig.cmake" file, as you can see here. However, an oldest version of OpenCV that I found on github have this file, as you can see here.
So, maybe some configuration is set to this oldest version and it's causing conflict? How to change that and get it working for the newest version? I think it must be something simple to solve but I'm quite newbie.
Thanks in advance.

CMake writes you what to do:
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files.
Another option - please, read comments in the file CMakeLists.txt that you've pasted, once more.
They say:
you may need to set OpenCV_DIR variable
to the absolute path to the directory containing OpenCVConfig.cmake
Another issue - in-source build, performed with passing dot to cmake. It is better to create separate directory for build files.
Building this way allows you easily adding your build directory to .gitignore and not polluting your source code repository with compiled binaries. Also, if something goes wrong, you can just remove that build directory and start again from scratch. That is much easier than messing with files one by one.
So, you need to call CMake like following
mkdir build
cd build
OpenCV_DIR=/home/dell/opencv cmake ..
or
mkdir build
cd build
cmake .. -DCMAKE_PREFIX_PATH=/home/dell/opencv
or the following also should do the trick
mkdir build
cd build
cmake .. -DOpenCV_DIR=/home/dell/opencv

Update your OpenCV search directory path as below,
find_package(OpenCV 3.4 REQUIRED PATHS "/usr/local/opencv/" NO_DEFAULT_PATH)

Related

Cmake find_package does not detect path in windows

I am trying to build the ceres sovler using Cmake_gui and Visual Studio
I have ceres-solver cloned to E:\Code\libs\ceres-solver
and the required library Eigen3 cloned to E:\Code\libs\eigen-3.3.9
When I run cmake-gui on ceres-solver, I get the following error:
-- Detected available Ceres threading models: [CXX_THREADS, OPENMP, NO_THREADS]
-- Building with C++14
CMake Error at CMakeLists.txt:242 (find_package):
By not providing "FindEigen3.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Eigen3", but
CMake did not find one.
Could not find a package configuration file provided by "Eigen3" (requested
version 3.3) with any of the following names:
Eigen3Config.cmake
eigen3-config.cmake
Add the installation prefix of "Eigen3" to CMAKE_PREFIX_PATH or set
"Eigen3_DIR" to a directory containing one of the above files. If "Eigen3"
provides a separate development package or SDK, be sure it has been
installed.
I modified ceres CMakeLists.txt line 241
from:
find_package(Eigen3 3.3 REQUIRED)
to
find_package(Eigen3 3.3 REQUIRED PATH "E:/Code/libs/eigen-3.3.9")
But I still get this error above
I also tried
list(APPEND CMAKE_PREFIX_PATH "E:/Code/libs/eigen-3.3.9")
set(Eigen3_DIR "E:/Code/libs/eigen-3.3.9")
My questions are:
What is the correct way to specify the path for find_package? Do I need to use path environment variable within windows or modify CMakeLists.txt?
Am I specifying this path correctly in Windows? Do I need to link to some internal directory of Eigen3? Like E:\Code\libs\eigen-3.3.9\cmake, use forward slashes instead of back slashes, or use quotation marks for the path? I tried all of these things without success.
Does find_package recursively search for the package within the directories specified? Or do I need to point to the exact directory?
Thanks
According to comments:
I must run Cmake-Gui on Eigen3 first, to generate a Build directory. However, it does not necessarily need to be built in VS afterwards,
Then setting on line 240 in CMakeLists.txt for ceres-solver
list(APPEND CMAKE_PREFIX_PATH "E:/Code/libs/eigen-3.3.9/build")
Was sufficient for Cmake-Gui to find the Eigen3Config.cmake file
Notes:
Eigen3 does not have any required dependencies it seems, but it does throw a lot of warnings when generating with Cmake-Gui, I ignored these
glog library or any other libraries are not required, but I did have to check "Enable mini-glog" option and re-run config for ceres-solver.

How to find (configure) Qt for cmake project of iOS?

I install Qt via online installer on my macOS (Qt for iOS and macOS).
And qmake project works just fine, now I need compile with cmake Qt project:
cmake_minimum_required(VERSION 3.5)
project(demo LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
And I can not find Qt.
Direct build configuration:
cmake .. -GXcode -DQt5_DIR=/Users/user/Qt/5.14.1/clang_64/lib/cmake/Qt5
...
-- Configuring done
-- Generating done
-- Build files have been written to:
Cross-compiling for iOS:
cmake .. -GXcode -DCMAKE_SYSTEM_NAME=iOS -DQt5_DIR=/Users/user/Qt/5.14.1/ios/lib/cmake/Qt5
...
CMake Error at /Users/user/Qt/5.14.1/ios/lib/cmake/Qt5/Qt5Config.cmake:28 (find_package):
Could not find a package configuration file provided by "Qt5Core" with any
of the following names:
Qt5CoreConfig.cmake
qt5core-config.cmake
Add the installation prefix of "Qt5Core" to CMAKE_PREFIX_PATH or set
"Qt5Core_DIR" to a directory containing one of the above files. If
"Qt5Core" provides a separate development package or SDK, be sure it has
been installed.
Obviously Qt5CoreConfig.cmake is just fine and it is in relatively the same place as for "direct" compilation, but for some reason cmake can not find it
$ find /Users/user/Qt/5.14.1/ios/lib/cmake/ -name Qt5CoreConfig.cmake
/Users/user/Qt/5.14.1/ios/lib/cmake//Qt5Core/Qt5CoreConfig.cmake
How can I fix this?
When building for iOS CMAKE_SYSROOT is set to iPhoneOS.sdk path.
find_package:
The CMAKE_SYSROOT variable can also be used to specify exactly one directory to use as a prefix. Setting CMAKE_SYSROOT also has other effects. See the documentation for that variable for more.
These variables are especially useful when cross-compiling to point to the root directory of the target environment and CMake will search there too. By default at first the directories listed in CMAKE_FIND_ROOT_PATH are searched, then the CMAKE_SYSROOT directory is searched, and then the non-rooted directories will be searched.
So, adding the option -DCMAKE_FIND_ROOT_PATH=/Users/user/Qt/5.14.1/ios will help.

CMake find_package for FindLibXml2

I'm trying to create a CMake file that will detect the location of libxml2. From what see in examples and CMake documentation the find_package simply works. I'm running CLion on Ubuntu, the libxml2 is installed using apt-get, the FindLibXml2.cmake is located under CMake's modules. However CMake returns cryptic message:
Could not find a package configuration file provided by "FindLibXml2"
with any of the following names:
FindLibXml2Config.cmake
findlibxml2-config.cmake
Add the installation prefix of "FindLibXml2" to CMAKE_PREFIX_PATH or
set "FindLibXml2_DIR" to a directory containing one of the above
files. If "FindLibXml2" provides a separate development package or
SDK, be sure it has been installed.
Why it is trying to find this -config file? what I'm doing wrong?
CMake snippet
find_package(FindLibXml2 CONFIG REQUIRED)
I've also tried
find_package(FindLibXml2 REQUIRED)
Not sure which one to use
You should not have the Find in FindLibXml2; do:
find_package(LibXml2 REQUIRED)
As explained in the documentation:
CMake searches for a file called Find<package>.cmake

Cmake error: by not providing "FindOpenCV.cmake" in CMAKE_MOUDLE_PATH

When I try to use cmake .., the Windows cmd points out an error:
CMake Error at CMakeLists.txt:15(find_package):
By not providing "FindOpenCV.cmake" in CMAKE_MOUDLE_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.
And I have searched many possible solutions online but it doesnt work...
For example:
write in command line cmake -DCMAKE_PREFIX_PATH="C:\OpenCV" ..
but it seems cmd doesnt know DCMAKE at all...
or cmake -DOpenCV_DIR="C:\OpenCV\build\x86\vc10\lib" .. also doesnt work.
I am not sure if those solutions are in Linux or Windows, because my computer is windows7 system... I think the error has explained how to fix it, which is
Add the installation prefix of "OpenCV" to CAMKE_PREFIX_PATH or set "OpenCV_DIR" to a directory containing one of the above files.
but I don't know exactly how to write the command code...
Please give me some hint...
A quick solution might be to edit the CMakeLists.txt to include the path for the OpenCV_DIR variable by adding the following line before the find_package(OpenCV REQUIRED):
set("OpenCV_DIR" "C:\OpenCV\build\x86\vc10\lib")
Folder containing the OpenCVConfig.cmake can also be found by:
find / -name "OpenCVConfig.cmake"

CMake find_package not finding Find<package>.cmake

I made and installed the aruco library, which put a Findaruco.cmake file in the /usr/local/lib/cmake directory. In my CMakeLists.txt file I have
...
find_package(aruco REQUIRED)
and it always returns the standard error
By not providing "Findaruco.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "aruco", but
CMake did not find one.
Could not find a package configuration file provided by "aruco" with any of
the following names:
arucoConfig.cmake
aruco-config.cmake
Add the installation prefix of "aruco" to CMAKE_PREFIX_PATH or set
"aruco_DIR" to a directory containing one of the above files. If "aruco"
provides a separate development package or SDK, be sure it has been
installed.
I've set the environment variable $CMAKE_PREFIX_PATH to each of the following, and none work
/usr/local
/usr/local/lib
/usr/local/lib/cmake
The only thing that works is setting the following in CMakeLists
set(CMAKE_MODULE_PATH /usr/local/lib/cmake)
I'm not sure what I'm doing wrong
Try setting CMake variable called CMAKE_PREFIX_PATH, not the environment one. Use -D flag during cmake invocation:
cmake -D CMAKE_PREFIX_PATH=/usr/local/lib <path to source or build dir>
But AFAIR, CMake should look into /usr/local prefix as its default behavior.