Ask Your Question
0

Error using cmake on Windows with Visual Studio 2017 and OpenCV 3.4.3

asked 2018-09-29 02:52:47 -0600

zscore gravatar image

updated 2018-09-29 05:35:18 -0600

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
edit retag flag offensive close merge delete

Comments

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.

fifo_thekdi gravatar imagefifo_thekdi ( 2018-10-01 14:52:14 -0600 )edit

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

fifo_thekdi gravatar imagefifo_thekdi ( 2018-10-01 14:54:43 -0600 )edit

5 is the only valid point here.

berak gravatar imageberak ( 2018-10-01 20:38:02 -0600 )edit

by valid you mean "related to the answer" or "it is simply wrong"?

fifo_thekdi gravatar imagefifo_thekdi ( 2018-10-01 20:46:44 -0600 )edit

2 answers

Sort by » oldest newest most voted
0

answered 2018-09-29 03:21:03 -0600

berak gravatar image

updated 2018-09-29 03:29:38 -0600

oh my, so cmake got both runtime and arch wrong. (x86 does not even exist, no more 32bit prebuilt libs)

i don't think, it's your fault, it all looks quite correct.

it's probably easier, to setup some "template" project manually now, and re-use that later, than fighting cmake.

  • create an empty project.
  • add C:\OpenCV34\opencv\build\include to "additional include dirs" (compiler)
  • add C:\OpenCV34\opencv\build\x64\vc15\lib to "additional library dirs" (linker)
  • add opencv_world.lib to "additional libraries" (linker, idk, how it's called exactly) for RELEASE
  • add opencv_worldd.lib to "additional libraries" (linker) for DEBUG
  • add your cpp file to the project
  • you also need to expand the PATH with C:\OpenCV34\opencv\build\x64\vc15\bin, so your exe can find the opencv_world.dll later
edit flag offensive delete link more

Comments

1

oh I know how to do that. Someone suggested to me that cmake is way easier and wanted to try that. Check the edit in Question, I was able to make some progress

zscore gravatar imagezscore ( 2018-09-29 05:29:54 -0600 )edit

It's not correct. the OpenCV_DIR was pointing to the wrong directory. Please read the points above. There is nothing wrong neither in OpenCV nor in CMake

fifo_thekdi gravatar imagefifo_thekdi ( 2018-10-01 14:58:00 -0600 )edit

read the question again, please.

berak gravatar imageberak ( 2018-10-01 20:35:24 -0600 )edit

this whole question was in reply to a comment I made on Reddit I've already given a complete solution with a video I made today I'll just add a full answer separately then

fifo_thekdi gravatar imagefifo_thekdi ( 2018-10-01 20:42:59 -0600 )edit

that's nice, but all of it useless here

berak gravatar imageberak ( 2018-10-01 20:45:12 -0600 )edit
1

please refer to this topic: https://github.com/opencv/opencv/issu... you were just wrong, plain and simple

fifo_thekdi gravatar imagefifo_thekdi ( 2018-10-01 21:02:21 -0600 )edit

that's a nice and helpful find !

berak gravatar imageberak ( 2018-10-01 21:15:07 -0600 )edit
-1

answered 2018-10-01 14:43:20 -0600

fifo_thekdi gravatar image

updated 2018-10-01 20:44:44 -0600

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

I made this short tutorial do demonstrate how you can we can create a new OpenCV based project on Windows using CMake. Using CMake for this task makes it much easier to target any platform or compiler simply by selecting it from CMake GUI, in addition to requiring no further configuration on other operating systems (Linux, MacOS, etc....). It is especially easy on Linux since CMake's find_package would pick up OpenCV directories automatically without any further configuration

Tools Used:

CMake 3.12.2

Visual Studio 2017

OpenCV 3.4.2

First, here is the demo video I've just made:

https://youtu.be/dQ0sq63l4AE

Here are the steps:

1- Go to https://github.com/opencv/opencv/rele... and download the latest release. In my case I downloaded opencv-3.4.2-vc14_vc15.exe

2- Extract the archive. Prefer the root of drive because long file names can generate errors when extracted in subfolders.

3- Point the environmental variable Opencv_DIR to the opencv\build directory

4- Add the directory containing the opencv_world343.dll to your PATH environmental variable OR just copy the DLL files to your system32 directory.

5- The test code I used is based on this project:

https://github.com/blueskymonster/ope...

6- Fire CMake GUI and add the root directory of the project to "source code" and add your own build directory (NOT THE SAME ONE USED FOR SOURCE CODE!). That's where the generated Visual Studio solution files and built binaries would reside.

7- Click Configure, and choose the compiler you would use. Pay attention for x64 or 32bit version as the default release comes only with the x64 vesion.

8- Click generate and Open Project

9- Build in Visual Studio

10- Run application. Note that to run the application from Visual Studio you would have to set DisplayImage as startup project. Otherwise you can run it from command-line or simply double click on the executable file ... (more)

edit flag offensive delete link more

Comments

link-only answer

berak gravatar imageberak ( 2018-10-01 20:31:46 -0600 )edit

answer edited

fifo_thekdi gravatar imagefifo_thekdi ( 2018-10-01 20:44:57 -0600 )edit

thank you !

berak gravatar imageberak ( 2018-10-01 20:47:00 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-09-29 02:52:47 -0600

Seen: 5,377 times

Last updated: Oct 01 '18