Getting the OpenCV library to work with C++ [closed]

asked 2018-03-16 07:37:16 -0500

quina gravatar image

updated 2018-03-16 09:13:00 -0500

Hello everyone,

just a short heads-up: I have never used OpenCV and barely programmed outside of university classes (that means I know all the basic concepts like loops, data types, object orientation etc but I am having a hard time dealing with all the stuff that surrounds the coding like getting libraries to work, setting up frameworks, ...)

I have been trying to get OpenCV to work for hours now, but I just cannot do it. The first thing I did is downloading and extracting the current OCV - 3.4.1. I wanted to get it to work in Code Blocks and after some research I learned that there are 'source' files from which I can build 'binaries' (correct me if I got that wrong) to use in my IDE. And that I need cmake to generate those binaries. So I downloaded it and tinkered around with it for hours - it just wouldn't work. I always got an error message telling me that some DLL (libintl-8?) was missing - even though it was exactly where it needed to be, inside the MinGW folder. Creating an OpenCV-project with Code Block did not work at all for me, I got stuck with some error messages I can't quite remember after linking the OpenCV-folder.

After that I read that I would not need to create the binaries myself if I used Visual C++, so I decided to download it and give it a try. I set my project's propertysheet (I hope that's the english name for it, I'm using it in a different language) "Linker" -> "Additional Library directories" to C:\opencv\build and the "C/C++" -> "Additional Include directories" to C:\opencv\build\include. Lastly, under "Linker" -> "Input" -> "Additional dependencies" I entered "opencv_world341.lib", because the full list (as described here: wouldn't work but it seems that this 'world' thing seems to contain all I need.

After typing #include, the IDE automatically suggests the opencv and opencv2 directories, so that seemed to work. However, after trying out the test-code from the link I posted above:

#include "stdafx.h"
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
    if (argc != 2)
        cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
        return -1;

    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file

    if (! // Check for invalid input
        cout << "Could not open or find the image" << std::endl;
        return -1;

    namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
    imshow("Display window", image); // Show our image inside it.

    waitKey(0); // Wait for a keystroke in the window
    return 0;

and run it, I get the following error:

1>------ Erstellen gestartet: Projekt: OpenCV_Test_Console, Konfiguration: Debug Win32 ------ 1>opencv_world341.lib(opencv_world341.dll) : fatal error LNK1112: Modul-Computertyp "x64" steht in Konflikt mit dem Zielcomputertyp "x86". 1>Die Erstellung des Projekts "OpenCV_Test_Console.vcxproj" ist abgeschlossen -- FEHLER. ========== Erstellen: 0 ... (more)

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by quina
close date 2018-03-22 07:02:21.631952


if you're using VS now, can we remove the codeblocks story ? it's irrelevant & confusing.

the error says, you built 64 bit libraries, but your project is 32bit. there's a dropbox in the toolbar to change this, look it up

berak gravatar imageberak ( 2018-03-16 08:43:37 -0500 )edit

I just posted it because I thought that maybe there is an easy way to get it to work in Code Blocks and that I'm just too much of a noob to figure it out (especially since there's this OpenCV project-type that just refuses to work).

quina gravatar imagequina ( 2018-03-16 08:45:19 -0500 )edit

After setting this to x64, I get the error

1>------ Erstellen gestartet: Projekt: OpenCV_Test_Console, Konfiguration: Debug x64 ------ 1>stdafx.cpp 1>OpenCV_Test_Console.cpp 1>OpenCV_Test_Console.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""void __cdecl cv::imshow(class cv::String const &,class cv::debug_build_guard::_InputArray const &)" (?imshow@cv@@YAXAEBVString@1@AEBV_InputArray@debug_build_guard@1@@Z)" in Funktion "main". 1>C:.......+\Projekte\OpenCV_Test_Console\x64\Debug\OpenCV_Test_Console.exe : fatal error LNK1120: 1 nicht aufgelöste Externe 1>Die Erstellung des Projekts "OpenCV_Test_Console.vcxproj" ist abgeschlossen -- FEHLER. ========== Erstellen: 0 erfolgreich, 1 fehlerhaft, 0 aktuell, 0 übersprungen ==========

quina gravatar imagequina ( 2018-03-16 08:51:50 -0500 )edit

you have to add opencv_world341.lib to the linker library list, not the dll

berak gravatar imageberak ( 2018-03-16 09:12:11 -0500 )edit

I thought that's what I did:

For some reason though, I get another error now: it simply says it cannot open the file opencv_world341.lib:

1>LINK : fatal error LNK1104: Datei "opencv_world341.lib" kann nicht geöffnet werden.

So I tried to link the exact directory the lib is located in the Linker -> Additional Library Directories (speaking of that, what does "vc14" and "vc15" mean and what's the difference?). But then I'll get yet another error:

1>OpenCV_Test_Console.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""void __cdecl cv::imshow(class cv::String const &,class cv::debug_build_guard::_InputArray const &)" (?imshow@cv@@YAXAEBVString@1@AEBV_InputArray@debug_build_guard@1@@Z)" in Funktion "main". [...]

quina gravatar imagequina ( 2018-03-16 09:32:30 -0500 )edit

i know, it's painful to be a noob.

opencv_world341d.lib for the debug version

berak gravatar imageberak ( 2018-03-16 09:36:54 -0500 )edit

hi berak,

thanks a lot for you continued support. sorry i couldn't answser earlier, i was out of town over the weekend. after specifying the debug-dll in den linker, the program seems to compile, but i now get yet another error (which is a runtime error this time, i suppose?

the error says that the code execution cannot be continued because opencv_world341d.dll could not be found. this is so strange since it is another missing dll error, already got one very similar sounding in cmake. and the dll IS in the folder, i just double checked it. here it says i should copy the dll into my VS\x64\release directory. doesnt work for me tho

quina gravatar imagequina ( 2018-03-19 06:32:48 -0500 )edit

yea, you hit every pothole there is.

the folder , where the dll is, has to go on the PATH env variable. do NOT try to copy dlls around, that's moronic. (you'll have more than 1 opencv program, and you wold have to do that every time, for both release and debug)

berak gravatar imageberak ( 2018-03-19 06:43:10 -0500 )edit

hi, i already tried to add it to my env variables (since cmake threw the same error, i'm already a little used to that) - didnt work for me either, though. i am not sure however, if i did this correctly. is it really just adding the folder to the path like so? i did the same in the system variables - still does not work. might a reboot be necessary? takes my computer forever, that's why i haven't already tried it :)

quina gravatar imagequina ( 2018-03-19 06:49:29 -0500 )edit

you'll need to restart the console(and VS, too), so it starts with the changed environment

type PATH in a cmd window, to see, what you have

berak gravatar imageberak ( 2018-03-19 07:04:38 -0500 )edit

i tried a system reboot in the meantime, still does not work. path gives me:

PATH=[...........];C:\opencv\build\x64\vc15\lib;C:\Users\quina\AppData\Local\Microsoft\WindowsApps;"C:\OpenCV\build\x64\vc10\bin;C:\OpenCV\build\common\tbb\intel64\vc10";C:\Program Files (x86)\CodeBlocks\MinGW\bin;C:\opencv\build\x64\vc15\lib;

quina gravatar imagequina ( 2018-03-19 07:19:21 -0500 )edit

C:\opencv\build\x64\vc15\bin not lib

berak gravatar imageberak ( 2018-03-19 07:20:34 -0500 )edit

the error-windows about missing the missing dll disappeared - thank you! why do i need to use the dll in the bin directory now when i specified the lib directory in the linker?

however, the code still does not execute. i copied the opencv logo in the project directory, upon executing the code with f5 (debugging) i get 58 lines of errors about other dlls that cannot be opened, most of them in the system32 folder ....

OpenCV_Test_Console.exe" (Win32): "C:\Windows\System32\ntdll.dll" geladen. PDB-Datei wurde nicht gefunden oder konnte nicht geöffnet werden.

i am also not quite sure what gets passed into the main's argv when executing this code in the IDE.

edit: after looking this up, i learned that those "errors" are not relevant and that the program executes as it should (next comment)

quina gravatar imagequina ( 2018-03-19 08:36:02 -0500 )edit

so these "errors" seem to be irrelevant, is this correct? when executing the program w/o debugging, it shows Usage: display_image ImageToLoadAndDisplay which is unsurprising since no arguments are passed. but how can i pass those in vs? do i have to use the functions display_image and ImageToLoadAndDisplay?

quina gravatar imagequina ( 2018-03-19 08:50:39 -0500 )edit
  1. run it from a cmdline, pass args there
  2. setup args / path in the "debug properties" section of VS
  3. hardcode it in the cpp

the pdb's are only for debugging the opencv libs. you only have those, if you built from src, locally. safe to ignore, yes.

berak gravatar imageberak ( 2018-03-19 08:53:08 -0500 )edit

works now, thanks!

quina gravatar imagequina ( 2018-03-22 07:01:49 -0500 )edit