Sunday, January 5, 2014

Building OpenCV 64 bit applications with Visual C++ 2010 Express

I've seen instructions/tutorials on how to build 32 bit OpenCV applications using Visual C++ 2010 Express and 64 bit OpenCV applications using the full-fledged Visual Studio 2010, but no instructions on building 64 bit OpenCV applications using Visual C++ 2010 Express. Hence, this post.


Platform: Windows 7 64 bit.
OpenCV: 2.4.8 (latest as of 04 January 2014).

1


Download and Install Microsoft Visual C++ 2010 Express Integrated Development Environment (IDE).


2

If there are any existing Microsoft Visual C++ 2010 Redistributables (both x86 and x64) then Uninstall them using Windows Control Panel otherwise they will cause the next step (Win SDK install) to fail (after the huge SDK has downloaded).




3

Visual C++ 2010 Express only contains the 32 bit C++ Compiler. To get the 64 bit compiler, one needs to install Windows 7 SDK. This contains Windows headers, dev libs, compilers, samples, tools and assorted utilities. Choose to install the headers, libs, compilers and tools as shown in figure below.

With above selections, download size is approx. 350 MB.



4

Download and install opencv-2.4.8.exe (345 MB) from OpenCV website. This contains OpenCV source, includes and pre-compiled libs (both x86 and x64) . All that the install step does is extract the files into a folder. Move it to usual libs folder viz. D:\PROJECTS\LIBRARIES\c\opencv
The top-level folder contains build and sources sub folders. The build folder contains the includes and libs which are required to build user applications in the IDE.


5

5.1

Start the IDE and create a new Empty Project.

5.2

Set the project Configuration to Debug and Platform to x64 as shown in figure below.




5.3

Now set Windows 7 SDK as the toolset for the project. If this is not done then 64 bit libs won’t be found and you can get linker errors like
LINK : fatal error LNK1104: cannot open file 'kernel32.lib'

Right-click project -> Properties ->  Configuration Properties -> General -> Platform Toolset = Windows7.1SDK



Among other things, what it does is sets VC++ Directories -> Library Directories to first point to $(WindowsSdkDir)lib\x64 so that linker gets the proper libs.


5.4

Now add references to the include and lib paths.
Right-click the project -> Properties -> VC++ Directories then add the include and lib paths.
D:\PROJECTS\LIBRARIES\c\opencv\build\include
D:\PROJECTS\LIBRARIES\c\opencv\build\x64\vc10\lib




5.5

Now specify each required OpenCV lib in project
Properties -> Linker -> Input -> Additional Dependencies
The libs are –
opencv_calib3d248d.lib
opencv_contrib248d.lib
opencv_core248d.lib
opencv_features2d248d.lib
opencv_flann248d.lib
opencv_gpu248d.lib
opencv_highgui248d.lib
opencv_imgproc248d.lib
opencv_legacy248d.lib
opencv_ml248d.lib
opencv_nonfree248d.lib
opencv_objdetect248d.lib
opencv_photo248d.lib
opencv_stitching248d.lib
opencv_ts248d.lib
opencv_video248d.lib
opencv_videostab248d.lib



The d at the end of the lib name indicates that these libs contain symbols to aid debugging.


6

6.1

Now create a test cpp file to check that everything is working ok.
Project Add -> New Item -> C++ File


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat image = imread("D:/PROJECTS/LIBRARIES/c/opencv/lena.jpg");
    
 if (!image.data) 
    {
        cout << "Unable to load image" << endl;
        return -1;
    }

    imshow("TestCV2", image);

    waitKey(0);
}

Give correct path to any chosen image file.

Now run (with debug) the project by pressing F5. A window containing given image should open.



6.2

If it’s required to create a Release mode (i.e. without debugging symbols) executable then change the project Configuration in step 5.2 to Release. Then repeat all the steps to add include and lib paths and the individual libs. This time choose the libs whose names don’t end with d.
Run (without debug) the project by pressing Ctrl + F5.


References



See Also





4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Works in Release but doesn't in Debug, any idea ? (error code 0xc000007b) Thx.

    ReplyDelete
    Replies
    1. I haven't encountered this error but by searching on the net, it seems it could be due to project (in Debug mode in your case) being set to Win32 and using 64-bit libs.

      Delete