OpenCV 3.4.1, first tutorial won't build

asked 2018-06-05 12:03:45 -0500

This is on Windows 10 x64, with Visual Studio 2017 and opencv 3.4.1.

I installed opencv via this guide from the docs, changing the git lines to add --branch 3.4 and changing the Visual Studio version to Visual Studio 15 2017. Everything appears to build fine, and I add opencv to path and make the local property pages per this page from the guide.

There are no red underlines or other indications that anything is wrong in the editor view, but upon trying to build the sample code given, more or less everything is an error. The build output is as follows:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------ 1>ConsoleApplication1.cpp

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(7): error C2871: 'cv': a namespace with this name does not exist

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(13): error C2065: 'cout': undeclared identifier

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(13): error C2065: 'endl': undeclared identifier

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(16): error C2065: 'Mat': undeclared identifier

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(16): error C2146: syntax error: missing ';' before identifier 'image'

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(16): error C2065: 'image': undeclared identifier

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(17): error C2065: 'image': undeclared identifier

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(17): error C2065: 'IMREAD_COLOR': undeclared identifier

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(17): error C3861: 'imread': identifier not found

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(18): error C2065: 'image': undeclared identifier

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(18): error C2228: left of '.empty' must have class/struct/union

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(18): note: type is 'unknown-type'

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(20): error C2065: 'cout': undeclared identifier

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(20): error C2039: 'endl': is not a member of 'std'

1>c:\users\jakob\source\repos\consoleapplication1\predefined c++ types (compiler internal)(238): note: see declaration of 'std'

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(20): error C2065: 'endl': undeclared identifier

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(23): error C2065: 'WINDOW_AUTOSIZE': undeclared identifier

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(23): error C3861: 'namedWindow': identifier not found

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(24): error C2065: 'image': undeclared identifier

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(24): error C3861: 'imshow': identifier not found

1>c:\users\jakob\source\repos\consoleapplication1\consoleapplication1.cpp(25): error C3861: 'waitKey': identifier not found

1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.

VS clearly sees the headers, since it doesn't give the red underlines to indicate that they're not found. Still, something is clearly very wrong. I couldn't find anything by searching here, sorry if I missed something.

edit retag flag offensive close merge delete

Comments

"changing the git lines to add --branch 3.4" What do you mean ? (CMAKE_CONFIG_GENERATOR="Visual Studio 15 2017 Win64")

"Windows 10 x64, with Visual Studio 2017 and opencv 3.4.1" and "Configuration: Debug Win32 ------ " ? it should be Debug x64

LBerger gravatar imageLBerger ( 2018-06-05 12:39:05 -0500 )edit

Thank you for responding! In the linked guide, there is a suggested sh script to download and build opencv. The lines in this script to get the opencv files pull from the opencv and opencv_contrib repos' 'master' branches. These branches now hold opencv 4.0.0, so I changed the lines

git clone https://github.com/opencv/opencv.git

and

git clone https://github.com/opencv/opencv_cont...

to

git clone https://github.com/opencv/opencv.git --branch 3.4

git clone https://github.com/opencv/opencv_cont... --branch 3.4

so that I download and build opencv 3.4.1 instead of 4.0.0-pre.

Whether I tried to build for Win32 or x64 did not affect the errors, I first tried Debug x64 without luck. I should have written this in the question, my bad there.

jakobkg gravatar imagejakobkg ( 2018-06-05 12:55:47 -0500 )edit

"there is a suggested sh script " Ok that's my script! opencv build process it's about 20 minutes. CMAKE_CONFIG_GENERATOR="Visual Studio 15 2017 Win64"

Now if you build opencv using cmake and vs2017 you should try to build this sample and you use cmake-gui to build your vs.sln solution

LBerger gravatar imageLBerger ( 2018-06-05 13:06:37 -0500 )edit

Since I posted, I have tried re-building from source to no avail, and I have tried downloading the provided self-extracting package from here and set my environment variables to point at the files from it, which also didn't change anything.

jakobkg gravatar imagejakobkg ( 2018-06-05 13:07:22 -0500 )edit

Thanks again! I was not able to build that sample, building it with VS2017 again fails and gives a couple of errors.This time the errors are about not finding OpenCVConfig.cmake or opencv-config.cmake, saying I should set OpenCV_DIR to point to a directory containing one of these files. I have an environment variable like guide I linked told me to, set with

setx -m OPENCV_DIR D:\opencv\build\x64\vc15

This directory holds nothing but two subfolders, /bin and /lib.

jakobkg gravatar imagejakobkg ( 2018-06-05 13:33:37 -0500 )edit

No you must give D:/opencv/build to cmake or if you install opencv path where you install opencv (in script it is $repo//install/opencv

LBerger gravatar imageLBerger ( 2018-06-05 13:58:38 -0500 )edit

@jakobkg

setx -m OPENCV_DIR D:\opencv\build\x64\vc15

this is the path to your OLD (prebuild) binaries. to use the new, locally built ones change to:

> setx -m OPENCV_DIR D:\opencv\build\install\x64\vc15

setx -m OPENCV_DIR D:\opencv\build\install

berak gravatar imageberak ( 2018-06-06 01:11:44 -0500 )edit

Using

setx -m OPENCV_DIR D:\opencv\build\install\x64\vc15

does not appear to help, cmake still gives errors about not finding configs and building the tutorial sample in VS still gives all the original errors

jakobkg gravatar imagejakobkg ( 2018-06-06 05:32:51 -0500 )edit

apologies, it is:

setx -m OPENCV_DIR D:\opencv\build\install

and you have to append:

D:\opencv\build\install\x64\vc15\bin

to the PATH env variable, so it finds the dlls.

berak gravatar imageberak ( 2018-06-06 05:55:20 -0500 )edit

Alright sorry I'm being slow, I had some internet trouble yesterday. I rebuilt everything without using your sh script, with no luck. I'm going to dump as much info as I can into this comment, because I do not get this at all.

I did everything in a folder I called D:\OpenCV, so my install is located in D:\OpenCV\build\install.

I have appended "D:\OpenCV\build\install\x64\vc15\bin" to my PATH variable, and I have made an environment variable called OPENCV_DIR, which has "D:\OpenCV\build\install".

I have made two property sheets in Visual Studio, called OpenCV_Debug and OpenCV_Release.

In OpenCV_Debug, I have gone into "C/C++ - General" and added "$(OPENCV_DIR)\include" to "Additional include directories", gone to "Linker - General" and added "$(OPENCV_DIR)\x64\vc15\lib" as a library dir

jakobkg gravatar imagejakobkg ( 2018-06-07 10:43:39 -0500 )edit