Ask Your Question

Problem to set Visual Studio for OpenCV

asked 2016-06-02 12:05:56 -0500

VanGog gravatar image

updated 2016-06-03 13:30:44 -0500

I have followed the OpenCV guides for installation in Windows and setting of Visual Studio but I have problem if I understood all step correctly. The folder structure is different.

My system is Windows XP. IDE Visual Studio 2010. Downloaded and installed OpenCV 3.1

@echo %OPENCV_DIR%

notice: I have added both as system environment variable (+rebooted PC) and as user environment variable (+relogged user, no reboot).

here is my directory structure:

>cd \PROGRAMY\programming\OpenCV
>dir /b /o:d
build 3.1

>cd \PROGRAMY\programming\OpenCV\build 3.1
>dir /b /o:d

These folders I have moved from installation folder from build. I use 32 bit Windows, I skipped Java, Perl and 64 bit things. Note there is bin/opencv_ffmpeg310.dll and lib/opencv_ffmpeg310.dll - same size files - only dlls/lib I have. I expect this to be the opencv main library, right?

The P:\PROGRAMY\programming\OpenCV\build 3.1\include\ has two folders opencv and opencv2 ? Here it is not clear which folder should be included because in doc one one folder is shown.

>cd \PROGRAMY\programming\OpenCV\sources
>dir /b /o:d
>dir /b /o:d

here are all sources

In my project I have set Release and Debug profile: Linker -> General -> Additional Library Dependecies: $(OPENCV_DIR)\build 3.1\lib Linker -> Input .. no change here. Here are standard Windows libraries: kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) I tried to add one library but I found the lib files are not part of the distribution. (sad). I have read there is needed to add some libraries, but no *.lib files I found in the folders.

Now I have created the file main.cpp which includes these files:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

then I build and I got this error:

1>------ Build started: Project: OPEN CL, Configuration: Debug Win32 ------
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>U:\C++\openCV\test 00\Debug\OPEN CL.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Note that if I include both directories include/opencv and include/opencv2 it still generates same error.

What do I do wrong during installation that I am not able to compile the program in guide? Do I need to include some lib files?


Edit: After creating new clear project and setting include directories I can compile single program. But when I try to include OpenCV core so it will print error. Do I need to link any library? Which file? I cannot link opencv_ffmpeg310.dll it would print error that file is corrupt (There are two files like this, both error ... (more)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-06-03 12:18:24 -0500

pklab gravatar image

updated 2016-06-04 05:46:22 -0500

You can't use the the Pre-built OpenCV 3.1.0 with Visual Studio 2010. The Windows installation contains binary for VS2013 and VS2015 only.

EDIT : added some general infos

You have to build OpenCV from source following Installation by Making Your Own Libraries from the Source Files from 1st tutorial.

I strongly suggest to build also the OpenCV Install project. image description

If you have built the library in C:\OpenCV\Build\ than the Install project collects all needed files in C:\OpenCV\Build\install this is your OCV_ROOT path.

Define an env var that points to the install folder setx -m OCV_ROOT C:\OpenCV\Build\install, OR move the folder where you want:

mv C:\OpenCV\Build\install C:\yourfolder
setx -m OCV_ROOT C:\yourfolder

Configure a new project for OpenCV

This is for Visual Studio but the concept is still valid for each IDE. Try to understand 3 basic and general rules:

  1. The compiler needs to know where are the include files
    • Compiler > Include dir: $(OCV_ROOT)\include
  2. The linker needs to know where are the library files and which libraries has to use
    • Linker >Lib dir: $(OCV_ROOT)\$(PLATFORM)\vc10\lib (vc12 for VS2013, vc14 for VS2105)
    • Linker >Additional Lib Files > add all opencv_*(opencv_version).lib files available in the Lib dir. If you have built a debug version of OpenCV the files will be opencv_*(opencv_version)d.lib
  3. The executables need to reach the DLLs
    • copy all DLLs from $(OCV_ROOT)\$(PLATFORM)\vc10\bin into your executable folder
    • OR copy all DLLs in a folder listed in the %PATH% env var
    • OR add the folder that contains the DLLs to your %PATH% env var

Additional tips for your Visual Studio+OpenCV projects

  • Only pure C++ is allowed (I mean no .NET/C#, no managed code like /clr)
  • Use right Runtime Library: Multi-threaded DLL (/MD) for your Release project and Multi-threaded Debug DLL (/MDd) for your Debug project
  • You could create a basic project configured as above and export it as template. Than you can easily create a new project from the template to get an OpenCV ready project
  • You could set all needed configuration into a Visual Studio New Properties file than attach this file to a new project to easily add the OpenCV build rules.
edit flag offensive delete link more


Note I am using Windows XP, no program like setx. However I have compiled it already. Thanks

VanGog gravatar imageVanGog ( 2016-06-03 12:22:39 -0500 )edit

ok you should know that with XP you can define an env var with set OPENCV_ROOT=C:\yourfolder or using My Computer > Properties > Advanced > Env Vars

pklab gravatar imagepklab ( 2016-06-03 12:40:49 -0500 )edit

But what is the compilation of Install good for? Purpose? When I have tried to build it so VS reported me that no need to compile it becaase it is compiled already; that was the result of Built command 0 success, 0 failed.

VanGog gravatar imageVanGog ( 2016-06-04 06:07:25 -0500 )edit

@VanGog Read the answer ...the Install project collects...

pklab gravatar imagepklab ( 2016-06-04 06:11:24 -0500 )edit

@pklab: I have found that after build there is folder bin/Release which contains many *.ilk and *.pdb files which takes a lot of hard disk space. 846MB per OpenCV 3.1. And the bin/Debug files same type takes 476MB. May I delete these files? I don't think I need them for compilation of new C++ projects. Yet lib/Debug contains *.exp and *.pdb 26MB and lib/Release/25MB. If I could delete those files it would help me coz I have poor disk space on programs partition.

VanGog gravatar imageVanGog ( 2016-06-14 08:14:55 -0500 )edit

@VanGogShort answer: you can delete this files. Details: *.pdb files are needed to debug OpenCV itself. Sometime it's useful to going into the library to understand how it works. *.ilk files are Incremental Linking File. If you keep them you will save time in case of OpenCV rebuild.

pklab gravatar imagepklab ( 2016-06-16 12:08:29 -0500 )edit

@pklab thanks. Last question. is OpenCV_DIR the only environment variable which I need to set? When I set OpenCV_DIR in user account (not the system one), do I still need to add U:\OpenCV Build\3.1 win32+contrib\bin\Debug;U:\OpenCV Build\3.1 win32+contrib\bin\Release directories to PATH? I think the only one path should be needed so I could remove those last two paths

VanGog gravatar imageVanGog ( 2016-06-17 01:21:33 -0500 )edit

Please try to familiarize with the concepts in my answer.

If you build the "Install" project all needed files (including contrib) will be packaged in <build_dir>\Install . Set your OpenCV_DIR=<build_dir>\Install than use relative reference like $(OpenCV_DIR)\Include and others

pklab gravatar imagepklab ( 2016-06-17 02:02:22 -0500 )edit

Fine now it is clear

VanGog gravatar imageVanGog ( 2016-06-17 02:55:02 -0500 )edit

@pklab: Now I want to recompile OpenCV with TBB and I stuck this problem. What is opencv_config_file_include_dir? Should it be the destination directory or source directory? When I started cmake GUI there is set destination directory (the last settings used). And the path is U:/OpenCV Build/3.1 win32 + contrib ... and error I get is: CMake Error at cmake/OpenCVModule.cmake:260 (foreach) - syntax error during parsing string: U:/opencv-master/modules;EXTRA;U:\opencv_contrib-master\modules ... so I dont know where does it take the EXTRA word from.

VanGog gravatar imageVanGog ( 2016-06-26 04:15:46 -0500 )edit

Question Tools

1 follower


Asked: 2016-06-02 12:05:56 -0500

Seen: 1,960 times

Last updated: Jun 04 '16