Error using cmake on Windows with Visual Studio 2017 and OpenCV 3.4.3
Hi,
I am following this page from OpenCV Docs to use cmake with OpenCV.
This is my file structure,
- displayopencv\
-- CMakeLists.txt
-- DisplayImage.cpp
CMakeLists.txt contains,
cmake_minimum_required(VERSION 2.8)
set(OpenCV_DIR C:/OpenCV34/opencv/build)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
I run cmake .
in this directory inside command prompt and get the following error,
-- Selecting Windows SDK version 10.0.16299.0 to target Windows 10.0.17134.
-- OpenCV ARCH: x86
-- OpenCV RUNTIME: vc14
-- OpenCV STATIC: OFF
CMake Warning at C:/OpenCV34/opencv/build/OpenCVConfig.cmake:156 (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:4 (find_package)
CMake Error at CMakeLists.txt:4 (find_package):
Found package configuration file:
C:/OpenCV34/opencv/build/OpenCVConfig.cmake
but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be
NOT FOUND.
-- Configuring incomplete, errors occurred!
See also "E:/Code/cmake/displayopencv/CMakeFiles/CMakeOutput.log".
I cannot figure out what's wrong, it selected the OpenCV arch incorrect too. I have 64-bit OpenCV installed at that location,
C:\OpenCV34\opencv\build>dir
Volume in drive C has no label.
Volume Serial Number is E8D4-7C0B
Directory of C:\OpenCV34\opencv\build
01-09-2018 11:47 <DIR> .
01-09-2018 11:47 <DIR> ..
01-09-2018 11:49 <DIR> bin
01-09-2018 11:47 <DIR> etc
01-09-2018 11:47 <DIR> include
01-09-2018 11:47 <DIR> java
11-04-2018 07:48 2,275 LICENSE
29-08-2018 16:05 433 OpenCVConfig-version.cmake
29-08-2018 16:05 5,728 OpenCVConfig.cmake
01-09-2018 11:47 <DIR> python
01-09-2018 11:47 <DIR> x64
3 File(s) 8,436 bytes
8 Dir(s) 93,118,218,240 bytes free
C:\OpenCV34\opencv\build>dir x64
Volume in drive C has no label.
Volume Serial Number is E8D4-7C0B
Directory of C:\OpenCV34\opencv\build\x64
01-09-2018 11:47 <DIR> .
01-09-2018 11:47 <DIR> ..
01-09-2018 11:47 <DIR> vc14
01-09-2018 11:47 <DIR> vc15
0 File(s) 0 bytes
4 Dir(s) 93,118,218,240 bytes free
How do I fix this issue and use cmake with OpenCV on Windows 10 computer?
Edit 1:
I changed CMakeLists.txt and changed set(OpenCV_DIR...
line to this,
SET("OpenCV_DIR" "C:/OpenCV34/opencv/build/x64/vc15/lib")
After this,
E:\Code\cmake\DisplayImage>cmake .
-- Selecting Windows SDK version 10.0.16299.0 to target Windows 10.0.17134.
-- Found OpenCV: C:/OpenCV34/opencv/build (found version "3.4.3")
-- Configuring done
-- Generating done
-- Build files have been written to: E:/Code/cmake/DisplayImage
It seems to work but I cannot seem to compile the SLN generated in Visual Studio 2017. The default configuration was Win32, I copied the settings to x64 but I get this error
Error LNK1112 module machine type 'x64' conflicts with target machine type 'x86' DisplayImage E:\Code\cmake\DisplayImage\x64\Debug\DisplayImage.obj 1
There are some mistakes here:
1- OpenCV_DIR should point to the "build" directory, and not to "/build/x64/vc15/lib"
2- Avoid using that set command since you are going to distribute these files. Use Windows environmental variables instead.
3- I can see that you make a "cmake .", which is a big no no. You should never ever generate the solution files inside the directory that has CmakeLists.txt. You should create a new directory like "build" inside the DisplayImage directory, get inside that directory and use the command "cmake .."
4- I always prefer the CMake GUI version since it can give you all the possible combinations of variables and it makes it much easier for you to select target compiler and framework.
5- When you did the "cmake" command without any option it automatically selected x86, while the OpenCV directory variable was set to the x64 version. You could either could either pass it as a parameter in the command line, for example: cmake -G "Visual Studio 12 2013 Win64", or simply use the GUI and select your target compiler
5 is the only valid point here.
by valid you mean "related to the answer" or "it is simply wrong"?