By Elfoc

2011-08-10 13:05:36 8 Comments

I'm trying to use opencv 2.3 with Visual Studio 2010 Express. My code is from example:

#include "stdafx.h"
#include <highgui.h>

int _tmain(int argc, _TCHAR* argv[])
    int c;
    // allocate memory for an image
    IplImage *img;
    // capture from video device #1
    CvCapture* capture = cvCaptureFromCAM(1);
    // create a window to display the images
    cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
    // position the window
    cvMoveWindow("mainWin", 5, 5);
        // retrieve the captured frame
        // show the image in the window
        cvShowImage("mainWin", img );
        // wait 10 ms for a key to be pressed
        // escape key terminates program
        if(c == 27)         

    return 0;

What have I done so far?

  • Added build\bin and one of build\{x86|x64}\{vc9\vc10\mingw}\bin to my system path (to use DLLs).
  • Added build\{x86|x64}\{vc9\vc10\mingw}\lib or build\{x86|x64}\{vc9\vc10\mingw}\staticlib as library directories to my linker settings.
  • Added build\include and build\include\opencv as include directories to my compiler settings.

And the result is:

1>LINK : fatal error LNK1104: cannot open file 'c:\OpenCV2.3\build\x86\vc10\lib.obj'

There's no lib.obj in OpenCV folders. I've only unziped OpenCV-2.3.0-win-superpack.exe, without using CMake software.

What am I doing wrong?


@pimmes111 2012-04-10 08:51:18

If you are struggling with editing the PATH environment variables, you can also copy the required .dll files to your project folder: The dll files are located in this folder ../OpenCV2.3/build.x86/vc9/bin

Then copy them to the folder where .exe file is created: c:\Users\PIMMES\Documents\Visual Studio 2010\Projects\eigenfaces\Debug (Ofcourse you have to change the path to your Debug folder)

You only have to copy the .dll files which you are using in your project (#include for example) For example if you get an error message saying opencv_core231d.dll is not found then get this .dll file from the above location (bin folder) and copy to your project Debug folder.

Hope this helps..

@Snehasish 2013-07-17 21:04:15

Thanks for suggesting this alternate method. It worked flawlessly. I edited the Environment Path Variables correctly even though it was unable to detect the dll's correctly. Can you suggest me any possible reasons ?

@karlphillip 2011-08-10 17:11:10

Well, the official guide is for installing OpenCV 2.1 on VS2010, so I wrote some instructions below that shows how to properly install and configure the x86 version of OpenCV 2.3 on Visual Studio 2010 (Express), since a lot of folks seem to have problems setting it up correctly.

Download OpenCV-2.3.0-win-superpack.exe and execute it to extract all files to a folder named OpenCV2.3. Inside this folder there are 2 directories: build and opencv. All the setup on VS2010 will refer to the build directory. For practical purposes I moved the folder OpenCV2.3 to my C:\ drive, so pay attention to the paths I suggest on this guide as yours might be different.

On Visual Studio, create a new Win32 Console Application project and name it whatever you like. After that, a new window will show up. Click on the tab Application Settings and make sure the option Empty Project gets selected:

enter image description here

Add a new file main.cpp to the folder Source Files, then add this code to main.cpp:

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

int main(int argc, char* argv[])
if (argc < 2)
    printf("Usage: ./opencv_hello <file.png>\n");
    return -1;

    IplImage* img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
if (!img)
    return -1;

cvNamedWindow("display", CV_WINDOW_AUTOSIZE);
    cvShowImage("display", img );


    return 0;

At this point, we need to configure the project so it can locate OpenCV headers and libraries. Go to the Project Properties (ALT+F7), and once the new window shows up do the following:

  • On the Configuration box, select All Configurations

  • Open Configuration Properties > C/C++ > General, and edit the field Additional Include Directories to add these 3 paths (for the headers):




enter image description here

Note that include\opencv is for the C interface of OpenCV and include\opencv2 if for the C++ interface. We are also adding the folder include to prevent our build from being broken by some headers of the C interface that refer to C++ headers as opencv2\core.

  • Then, add the path of the libraries on Configuration Properties > Linker > General, and on the Additional Library Directories field, add this: C:\OpenCV2.3\build\x86\vc9\lib:

enter image description here

  • Finally, for this simple test we are going to add the libraries opencv_core230.lib and opencv_highgui230.lib. So go to Configuration Properties > Linker > Input and add them:

enter image description here

When writing more complex applications you'll probably need to add other OpenCV libs that I did not mentioned on this little project of ours.

Press F7 to Build Solution and you should see:

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

To be able to execute the application you'll need to modify the PATH environment variable of your system to add the location of OpenCV's DLLs. Add this to end of PATH:

; C:\OpenCV2.3\build\x86\vc9\bin

@karlphillip 2011-08-12 02:45:33

@Elfoc Drastically improved answer to show steb-by-step how to configure OpenCV 2.3 and VS2010.

@Kaushik Acharya 2012-01-28 19:35:14

I have added e:\opencv\build\x86\vc9\bin to system PATH envirenment variable. I am getting run time error: opencv_core231d.dll was not found .... I am using Visual studio 2008, Pentium 4 m/c. Any idea what's the mistake?

@Mohsen Afshin 2012-03-22 08:18:03

did you restart your system after editing PATH var. A reboot is required so that VS see the change.

@Abid Rahman K 2012-06-06 08:07:06

@karlphillip: Hi, 2.4.1 is released. Can you update your answer for that? Or is the procedure same. I tried as per this, but didn't work. Shows "opencv_core240d.dll is missing from the computer".

@karlphillip 2012-06-06 13:22:19

@AbidRahmanK I haven't installed 2.4.1 on Windows yet, I'll try it tonight and then I'll let you know if I succeeded.

@Abid Rahman K 2012-06-28 18:52:59

@karlphillip: it shows cannot open file 'opencv_contrib240d.lib'. What to do?

@karlphillip 2012-06-28 19:09:57

@AbidRahmanK The d at the end of the name indicates the debug version of the library. Maybe v2.4 don't include the debug version anymore and just the release. So try the name opencv_contrib240.lib instead.

@Abid Rahman K 2012-06-28 19:16:21

No. it was because i used 2.4.1. so it is 241 instead of 240. Now it is compiled, but shows another problem "Native' has exited with code -1 (0xffffffff)."

@karlphillip 2012-06-28 19:53:44

@AbidRahmanK That is a runtime error, something in the code failed. If you are using the code above, you'll see that it returns -1 when it can't load the image.

@Abid Rahman K 2012-06-29 15:13:00

@karlphillip: great my friend. Finally it worked for me. already +1-ed this answer. so +1 for your other link.

@rossb83 2011-08-10 16:40:31

Whenever I make a program that uses opencv 2.2 or greater I include everything, and then comment out the libraries I don't need. Try this, I'm sure you need more than highgui.h

#include "opencv2\opencv.hpp"

using namespace cv;

//#pragma comment(lib, "opencv/opencv_calib3d231.lib")
//#pragma comment(lib, "opencv/opencv_contrib231.lib")
#pragma comment(lib, "opencv/opencv_core231.lib")
//#pragma comment(lib, "opencv/opencv_features2d231.lib")
//#pragma comment(lib, "opencv/opencv_flann231.lib")
//#pragma comment(lib, "opencv/opencv_gpu231.lib")
//#pragma comment(lib, "opencv/opencv_haartraining_engine.lib")
#pragma comment(lib, "opencv/opencv_highgui231.lib")
//#pragma comment(lib, "opencv/opencv_imgproc231.lib")
//#pragma comment(lib, "opencv/opencv_legacy231.lib")
//#pragma comment(lib, "opencv/opencv_ml231.lib")
#pragma comment(lib, "opencv/opencv_objdetect231.lib")
//#pragma comment(lib, "opencv/opencv_ts231.lib")
//#pragma comment(lib, "opencv/opencv_video231.lib")

Related Questions

Sponsored Content

29 Answered Questions

[SOLVED] How do you auto format code in Visual Studio?

20 Answered Questions

[SOLVED] .gitignore for Visual Studio Projects and Solutions

12 Answered Questions

[SOLVED] How do I add an existing directory tree to a project in Visual Studio?

  • 2008-12-25 03:17:13
  • Brian Leahy
  • 396603 View
  • 841 Score
  • 12 Answer
  • Tags:   visual-studio

12 Answered Questions

4 Answered Questions

12 Answered Questions

[SOLVED] Can you force Visual Studio to always run as an Administrator in Windows 8?

12 Answered Questions

[SOLVED] Writing to output window of Visual Studio

21 Answered Questions

[SOLVED] Should I add the Visual Studio .suo and .user files to source control?

1 Answered Questions

Visual Studio 2010 windows 7 64bit create OpenCV & OpenGL project at the same time

Sponsored Content