3

I build the OpenCV 3.0 from source. And with the contrib repo for some extra modules. And I manually setup my VS2013 project to use the generated lib files.

My code is simple for now:

#include "opencv2\core\core.hpp"
#include "opencv2\imgcodecs\imgcodecs.hpp"

int _tmain(int argc, _TCHAR* argv[])
{
    cv::Mat image = cv::imread("img.jpg");
    return 0;
}

But it gave me these errors when in VS 2013 community version:

enter image description here

I see similar thread, they said it is caused by the x86/x64 issue. But My project is already x86. And the OpenCV I built is also targeting x86 (see below). What reason could it be?

enter image description here

ADD 1

The OpenCV 3.0 INSTALL project generates the following lib directory:

So it is indeed a x86 one.

enter image description here

My lib path is configured to the above path:

enter image description here

And I added all the *d.lib files.

enter image description here

And below is my VC project's Configuration Manager

enter image description here

So it is indeed x86, too.

Where could be wrong?

ADD 2

I manually searched for the fastFree() function. It is defined in the opencv_core300d.lib file. I use the dumpbin /symbols command to check the symbols. And I find its name is mangled exactly as fastFree@cv@@YAXPAX@Z. So why it cannot be found?

Community
  • 1
  • 1
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • 1
    it seems that you are compiling OpenCV from source. If so, do you build also INSTALL project that will put the libs in the correct position? Or you can just download the precompiled libs – Miki Jul 21 '15 at 14:51
  • @Miki Yes, I am compiling from source because I need to use some extra modules which is not included in the official build. And yes, I build the `INSTALL` project. That project put the libs in a place like this: `..\install\x86\vc12\staticlib`, which I added to my VC project as additional lib path. I added the screenshots to my question. – smwikipedia Jul 21 '15 at 14:56
  • I assume that you also added opencv_coreXXX etc.. in Linker -> Input -> Additional Dependencies, right? – Miki Jul 21 '15 at 15:01
  • @Miki Yes, I added them as `*d.lib`. Please see my newly added screenshot. – smwikipedia Jul 21 '15 at 15:21
  • @Miki I changed to use the official build of OpenCV 3.0.0. Exactly the same errors arose. – smwikipedia Jul 21 '15 at 15:31
  • I need to install OpenCV 3.0 also (I was using 2.4.9). I install them and come back to you – Miki Jul 21 '15 at 16:08
  • added the step to run the snippet. You probably forgot some of the required libs (you actually need a lot more than OpenCV 2.4.9, I got confused too). – Miki Jul 21 '15 at 17:09
  • I had this same problem, and it seemed that I was missing some library. By using the INSTALL target, I was able to collect all the necessary files, and the linker error went away. – Brian Stewart Feb 08 '19 at 05:19

1 Answers1

7

Here the steps to use OpenCV 3.0.0 with precompiled libs, for a C++ project that links OpenCV statically, in Windows (tested with Windows 8.1) and Visual Studio (tested with Visual Studio 2013) to run this program:

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    Mat3b img = imread("path_to_image");
    imshow("img", img);
    waitKey();
    return 0;
}
  1. Download from http://opencv.org/downloads.html
  2. Extract
  3. Let's call OPENCV_DIR the dir containing:
    • build
    • source
  4. Create an empty project:
    • New Project -> Visual C++ -> Empty Project
  5. Add a cpp file (say Start.cpp) that will contain your main function (e.g. the snippet above)
  6. Configuration DEBUG
  7. Add include and lib directories:
    • Configuration Properties -> VC++ Directories
    • Include Directories: add OPENCV_DIR\build\include
    • Library Directories: add OPENCV_DIR\build\x86\vc12\staticlib
  8. Add required libs (the following are good for this simple example, you should add more if you need other functionalities):

    • opencv_core300d.lib
    • opencv_highgui300d.lib
    • opencv_imgproc300d.lib
    • opencv_hal300d.lib
    • opencv_imgcodecs300d.lib
    • libjpegd.lib;
    • libpngd.lib
    • libjasperd.lib
    • IlmImfd.lib
    • libtiffd.lib
    • libwebpd.lib
    • zlibd.lib
    • ippicvmt.lib
    • %(AdditionalDependencies)
  9. Configuration Properties -> C/C++ -> Code Generation -> Runtime Library

    • Set to Multi-threaded Debug (/MTd)
  10. For a RELEASE build, you need to do steps from 6 to 9 in release mode, adding libs without the trailing "d" in step 8, and Multi threaded (/MT) in step 9.

  11. Enjoy!

As a bonus, I also recommend to install Image Watch extension for Visual Studio. Very very useful for debugging your Mats!

Miki
  • 40,887
  • 13
  • 123
  • 202
  • I compared your settings with mine. It seems I cannot use `*d.lib` as an abbreviation for all lib files. I changed to list the lib files respectively, and it compiles well now. I am using `VS2013 Community` version. Thanks. – smwikipedia Jul 22 '15 at 04:56
  • And also, I found `IlmImfd.lib` is missing from my build. In the official build, it is quite a big library(almost 28M) . But it still compiles without it. Not sure what it is for. – smwikipedia Jul 22 '15 at 04:58
  • Does not work for 3.3.1 : `fatal error LNK1104: cannot open file 'opencv_world311d.lib'` – Schütze Dec 19 '17 at 13:24