Error configuring OpenCV project with CMake on Windows - opencv

I used the prebuilt OpenCV 3.0.0 version. It works when I create a Visual Studio project directly, and when I add include and library directories.
Now I am making a CMakeList.txt file (so that I can run the same code on different systems). It will be used to create the Visual Studio project on Windows, and Makefile on Unixes, etc.
cmake_minimum_required(VERSION 2.8)
project( Facade )
find_package( OpenCV REQUIRED )
add_executable( Facade grammar.cpp )
target_link_libraries( Facade ${OpenCV_LIBS} )
When I generate the Visual Studio file on Windows, I got the following error:
CMake Warning at C:/opencv/build/OpenCVConfig.cmake:166 (message):
Found OpenCV Windows Pack but it has no binaries compatible with your
configuration.
You should manually point CMake variable OpenCV_DIR to your build of OpenCV
library.
Call Stack (most recent call first):
CMakeLists.txt:3 (find_package)
CMake Error at CMakeLists.txt:3 (find_package):
Found package configuration file:
C:/opencv/build/OpenCVConfig.cmake
but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be
NOT FOUND.
Currently, OpenCV_DIR is C:\opencv\build, where it holds the CMake files. If I point OpenCV_DIR to C:\opencv\build\x64\vc12, it says, can't find CMake files.
How do I fix this?

What generator string did you use for the CMake configuration step? For VC12 x64, you need to use -G "Visual Studio 12 Win64" (if you were using CMake 3, you would use -G "Visual Studio 12 2013 Win64"). Note that you need to include Win64 at the end to get the x64 platform.
It is rather convenient that the OpenCV CMake config script is checking to make sure you are using the x64 binaries. You configured CMake to build 32-bit binaries, which would cause a runtime error if you were able to get an executable built.

Just in case anybody else has this problem, I got it from using old CMake config scripts. With VS2017 installed, I was getting this error- turns out that some auto-generated CMAKE configs have something like this:
elseif(MSVC_VERSION EQUAL 1910)
set(OpenCV_RUNTIME vc15)
I checked MSVC_VERSION and it was 1916, which is still VC15, but the files erroneously missed that. I changed to:
elseif((MSVC_VERSION EQUAL 1910) OR (MSVC_VERSION GREATER 1910))
set(OpenCV_RUNTIME vc15)
and it worked like a charm.

Related

Could not identify shiboken version (5.15.2)

I try use combination from shiboken + C++ + Qt on Windows as my colleagues and it is not working
Console: x86 from MVS with administrator, QT 5.15.2 , CMAKE 3.19.8
Python from MVS installer 3.7 (use paths: PYTHONHOME C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64, path : C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64)
Shiboken download from https://code.qt.io/cgit/pyside/pyside-setup.git/
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug
C:\Users\...qt-5.15.2\pyside-setup\sources\shiboken2\ -DBUILD_TESTS=False
-DCMAKE_PREFIX_PATH='C:\Users\...\qt-5.15.2;' -DCMAKE_INSTALL_PREFIX='C:\Users\...\QT\shiboken2-debug' -
DPYTHON_EXECUTABLE='C:\ProgramFiles(x86)\MicrosoftVisualStudio\Shared\Python37_64\python_d.exe'
OUTPUT:
CMake Error at CMakeLists.txt:51 (message):
Could not identify shiboken version. Error:
when i change cmake file i saw interesting: PYTHON_DEBUG_LIBRARIES is empty
Having examined the cmakefile, I can saw that wherever the cmake calls the execute_process with python_d, python does not work and returns null, path to python_d is correct. I have python_d.exe in directory. I try install python not from MVS, and have same result. I try use 3.9 python. I cleaned sys paths from old python versions.
So, thanks colleague, it was a bad path DPYTHON_EXECUTABLE, i add message in Cmake file to output correct path, and successed used.
"C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python37_64/python_d.exe"
Before that I reinstalled the MVS, but I don’t think it had any effect

CMake fails to find OpenCV installed via vcpkg under wsl (returns opencv for Windows Conda Environment)

So I am currently trying to get my CMake Project to build after installing packages via vcpkg for the first time.
I am using WSL2 under Win10 with Ubuntu 20.04 for the project, but building throws the following:
philip#DESKTOP-2CHKCMO:/mnt/c/Users/phili/_Documents/Projects/pixelator$ cmake .
-- OpenCV ARCH:
-- OpenCV RUNTIME:
-- OpenCV STATIC: ON
CMake Warning at /mnt/c/Users/phili/anaconda3/Library/opencvConfig.cmake:140 (message):
Found OpenCV Windows Pack but it has no binaries compatible with your
configuration.
You should manually point CMake variable OpenCV_DIR to your build of OpenCV
library.
Call Stack (most recent call first):
CMakeLists.txt:13 (find_package)
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/c/Users/phili/_Documents/Projects/pixelator
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(pixelator LANGUAGES CXX)
#include("~/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(CMAKE_TOOLCHAIN_FILE "~/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(OpenCV_DIR "~/vcpkg/packages/opencv4_x64-linux/share/opencv")
find_package(protobuf CONFIG REQUIRED)
find_package(opencv CONFIG REQUIRED)
find_package(gflags CONFIG REQUIRED)
add_library(pixeling ${CMAKE_SOURCE_DIR}/source/pixeling.cpp)
target_link_libraries(pixeling ${OpenCV_LIBS})
add_executable(io ${CMAKE_SOURCE_DIR}/source/io.cpp)
target_link_libraries(io pixeling gflags)
The Folder is located in Windows10, but I am building via Ubuntu.
Apparently CMake seems to ignore that I set the OpenCV_DIR and returns an openCV installation for my python anaconda environment (which is not located in wsl, but Windows), which it can't use. Also, I was under the impression that I would not even be required to set the OpenCV_DIR after linking the Toolchain file?
How do I get it to find the right openCV installation?

Why doesn't CLion link my project with OpenCV for Windows?

I'm attempting to use OpenCV for Windows as supplied by opencv.org in a project I'm building with JetBrains' CLion IDE. I've installed the opencv library and configured CLion (set(OpenCV_DIR) to reference the build directory under it, but CMake issues the warning:
Found OpenCV Windows Pack but it has no binaries compatible with your configuration.
You should manually point CMake variable OpenCV_DIR to your build of OpenCV library.
I've tried some of the older distributions from opencv.org with the same results. It appears CMake is locating the OpenCV libraries, but doesn't want to use them. Why, and how do I get the OpenCV libraries to work under CLion?
The short answer is, you will probably need to build OpenCV from source in order to use it with CLion. But given the number and range of partially answered and unanswered questions here* and elsewhere on using JetBrains' CLion IDE with the OpenCV library, I think an overview is needed (my notes are from CLion 2016.3 and OpenCV 3.1, YMMV):
Though not produced by JetBrains, CMake is very central to CLion's operation. Understanding CMake therefore helps greatly in diagnosing CLion build problems. In particular CMake maintains a disk "cache" of settings which you may need to clear to incorporate changes to your environment (Tools->CMake->Reset Cache and Reload Project).
To make use of OpenCV in your build you must specify it in your project's CMakeLists.txt file. You request that CMake locate your OpenCV location and link it to your TARGET. An example of a sequence of commands from CMakeLists.txt for an executable named mushroom follows:
add_executable(mushroom ${SOURCE_FILES})
FIND_PACKAGE(OpenCV REQUIRED)
TARGET_LINK_LIBRARIES(mushroom ${OpenCV_LIBS})
(For more on FIND_PACKAGE, see CMake:How To Find Libraries.)
FIND_PACKAGE for package XXX works either by way of FindXXX.cmake files located at CMake's Modules directory, or by consulting environment variable XXXX_DIR. On my system, no FindOpenCV.cmake file was present, so I relied on the OpenCV_DIR environment variable instead. This must be set, not to the root of your OpenCV installation, but to the build folder beneath it. I used an entry in CMakeLists.txt to set this variable, e.g.:
set(OpenCV_DIR C:/Users/myacct/AppData/Local/opencv-3.0.0/build)
To link with OpenCV, CMake uses either FindOpenCV.cmake or OpenCV_DIR (see previous point above) to locate a file named OpenCVConfig.cmake. This file is generated by and ships with a particular build of OpenCV in order to document what components are present and where they are located.
Problems may occur when variable names used by OpenCVConfig.cmake conflict with those CLion has stored in its environment. In particular, if your OpenCV was built by Microsoft Visual C (MSVC), as is the Windows distribution from opencv.org, it won't work with CLion.
Because CLion's build toolchain (ControlAltS-toolchain) uses either MinGW or Cygwin, OpenCVConfig.cmake will search for OpenCV binaries under a subdirectory named mingw or cygwin and will find none because the binaries were built with MSVC (it will look in a directory like vc11 or vc12 instead). This probably means you will need to build OpenCV from source in order to use it with CLion.
Would reconfiguring OpenCVConfig.cmake to point to the MSVC binaries make this work? you may ask. Unfortunately the answer is still no, because libraries built with one compiler typically cannot be linked with another one.
OpenCVConfig.cmake or FindOpenCV.cmake likely contain diagnostic messages, but when CLion executes CMake for you, message(STATUS) calls are not displayed. To make them display, change them to message(WARNING) or message(FATAL_ERROR). But CLion 2016.3 EAP relieves this problem; see https://stackoverflow.com/a/39398373/5025060.
CLion does not indicate which .cmake script issued which diagnostics; don't assume they all come from the same script.
Hopefully this provides some general guidance on resolving CLion / CMake / OpenCV compatibility problems. Note that this does not cover compiler or linker issues; these will not appear until CMake completes its initial makefile build. Compiler or linker issues occur at a later stage and are controlled by include*(), link*() and other commands in CMakeLists.txt.
*Some related SO questions:
OpenCV Windows setup with CLion
OpenCV CLion (Cmake) linking issue - cmake reports a strange error
use OpenCV with Clion IDE on Windows
Compiling OpenCV on Windows with MinGW
Could not find module FindOpenCV.cmake ( Error in configuration process)
CMake: Of what use is find_package() if you need to specify CMAKE_MODULE_PATH anyway?

Building OpenCV using CMake problems

For using CUDA, I need to compile OpenCV. I'm using CMake and OpenCV 3 sources. I do not get any errors when clicking und "Generate" in CMake. Then I compile the OpenCV.sln solution for Win64 using Visual Studio (I selected the right visual studio version). I do not get any errors when compiling.
But I do know what to include; normally, there is "opencv" and "opencv2" in the include folder. But this does not exist.
My opencv folder after compiling:
My include folder:
Includes located in sources folder, not in build folder (if you did not Build INSTALL project).

Compiling LuaSocket: Cannot open file: 'string.h'

The log:
C:\WINDOWS\system32>luarocks install luasocket
Installing https://luarocks.org/luasocket-3.0rc1-2.src.rock...
Using https://luarocks.org/luasocket-3.0rc1-2.src.rock... switching to 'build' mode
cl /nologo /MD /O2 -c -Fosrc/mime.obj -Ic:/lua/include/ src/mime.c -DLUA_COMPAT_APIINTCASTS -DLUASOCKET_DEBUG -DNDEBUG -DLUASOCKET_API=__declspec(dllexport) -DMIME_API=__declspec(dllexport)
mime.c
src/mime.c(5): fatal error C1083: Cannot open include file: 'string.h': No such file or directory
Error: Build error: Failed compiling object src/mime.obj
I'm very new to the whole LuaRocks thing, so I have no idea how to go about fixing this.
Here's a non-complete guide to get you started on installing lua modules from a fresh Windows 7.
1) We need LuaRocks in order to install Lua dependencies automatically (similar to cpan/cpanm in Perl or pip in Python). Download LuaRocks by grabbing the latest version with a "-win32.zip" suffix from here: http://keplerproject.github.io/luarocks/releases/
At the time of this writing, this is the following file: http://keplerproject.github.io/luarocks/releases/luarocks-2.3.0-win32.zip
2) Unzip it into a new directory and install it with install.bat /L. Without the "/L" flag, it will complain that it cannot find your existing Lua installation. With the flag, it will just install a new Lua binary (version 5.1). LuaRocks is now installed to C:\Program Files (x86)\LuaRocks. You may now delete the extracted "luarocks-2.3.0-win32" directory.
3) In order to compile and install modules, we will need a C++ compiler. Download and install the Visual C++ Build Tools 2015 from here: http://landinghub.visualstudio.com/visual-cpp-build-tools
(We want this so that we can avoiding installing the huge, multi-gigabyte Visual Studio 2015.)
4) Navigate to C:\Program Files (x86)\Microsoft Visual C++ Build Tools. Right click on "Visual C++ 2015 x86 Native Build Tools Command Prompt", and select "Run as administrator".
5) Run the following command: "C:\Program Files (x86)\LuaRocks\luarocks.bat" install luasocket.
Note that you may have to install additional stuff for certain modules. And sometimes, modules are simply unable to be compiled on Windows at all.

Resources