undefind reference to 'cv::theRNG()' in OpenCV

Hey guys n gals,

I'm Ryan and this is my first post so a quick intro then on to the problem. I'm fresh outta uni and in my first programming job.
My background is in Java but the part of the project I am working on just now relies on OpenCV and hence C/C++ which I've been put in charge of because I used it at arms length in my disertation.
However I hardly had to touch the C code nevermind actually write my own functions in it so now I'm a bit stuck and in need of advice.

As the title suggests I'm having a problem with an undefind reference to 'cv::theRNG()' when trying to compile my project. I've spent the best part of the day trying to figure out what is going wrong but can't find it.
I know it is concerning the linking of libraries but I've linked everything I can think of and am no closer to solving the problem.

My setup is as follows:

Windows 7 on a Intel i5
Using Netbeans 7.0 for development
Using MinGW as my compiler (g++)
Using OpenCV2.3

Don't think there is any other system things you need to know but please ask if there is.

As for project setup with respect to OpenCV in Tools -> Options -> C/C++ -> Code Assistance -> C++ Compiler -> Include Directories I have C:\CCompiler\OpenCV2.3\include

In the project's properties I have:

Build -> C++ Compiler -> Include Directories ../../../../../CCompiler/OpenCV2.3/include
Additional Options '-Wl,--add-stdcall-alias'

Linker -> Additional Library Directories ../../../../../CCompiler/OpenCV2.3/lib
Libraries opencv_core230 opencv_highgui230 opencv_imgproc230 opencv_objdetect230 opencv_features2d (these are all dll files)
Additional Options '-Wl,--add-stdcall-alias'

And here is the code which is the convexhull example file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* 
 * File:   convexhull.cpp
 * Author: Ryan
 *
 * Created on 29 June 2011, 16:55
 */

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include <fstream>
#include <iostream>

using namespace cv;
using namespace std;

int main( int /*argc*/, char** /*argv*/ )
{
    Mat img(500, 500, CV_8UC3);
    RNG& rng = theRNG();

    for(;;)
    {
        char key;
        int i, count = (unsigned)rng%100 + 1;
        
        vector<Point> points;

        for( i = 0; i < count; i++ )
        {
            Point pt;
            pt.x = rng.uniform(img.cols/4, img.cols*3/4);
            pt.y = rng.uniform(img.rows/4, img.rows*3/4);
            
            points.push_back(pt);
        }
        
        vector<int> hull;
        convexHull(Mat(points), hull, true);
        
        img = Scalar::all(0);
        for( i = 0; i < count; i++ )
            circle(img, points[i], 3, Scalar(0, 0, 255), CV_FILLED, CV_AA);
        
        int hullcount = (int)hull.size();
        Point pt0 = points[hull[hullcount-1]];
        
        for( i = 0; i < hullcount; i++ )
        {
            Point pt = points[hull[i]];
            line(img, pt0, pt, Scalar(0, 255, 0), 1, CV_AA);
            pt0 = pt;
        }

        imshow("hull", img);

        key = (char)waitKey();
        if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
            break;
    }

    return 0;
}

and here is the actual errors I'm getting:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"/bin/make" -f nbproject/Makefile-Facedetect.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/c/Users/Ryan/NetBeansProjects/Technabling/CppTutorials'
"/bin/make"  -f nbproject/Makefile-Facedetect.mk dist/Facedetect/MinGW-Windows/cpptutorials.exe
make[2]: Entering directory `/c/Users/Ryan/NetBeansProjects/Technabling/CppTutorials'
mkdir -p build/Facedetect/MinGW-Windows
rm -f build/Facedetect/MinGW-Windows/handDetection.o.d
g++.exe -Wl,--add-stdcall-alias   -c -g -I../../../../../CCompiler/OpenCV2.3/include -MMD -MP -MF build/Facedetect/MinGW-Windows/handDetection.o.d -o build/Facedetect/MinGW-Windows/handDetection.o handDetection.cpp
mkdir -p dist/Facedetect/MinGW-Windows
g++.exe -Wl,--add-stdcall-alias    -Wl,--add-stdcall-alias -o dist/Facedetect/MinGW-Windows/cpptutorials build/Facedetect/MinGW-Windows/main.o build/Facedetect/MinGW-Windows/facedetect.o build/Facedetect/MinGW-Windows/convexhull.o build/Facedetect/MinGW-Windows/skindetect1.o build/Facedetect/MinGW-Windows/main2.o build/Facedetect/MinGW-Windows/handDetection.o -L../../../../../CCompiler/OpenCV2.3/lib -lopencv_core230 -lopencv_highgui230 -lopencv_imgproc230 -lopencv_objdetect230 -lopencv_features2d230 
build/Facedetect/MinGW-Windows/convexhull.o: In function `main':
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/convexhull.cpp:22: undefined reference to `cv::theRNG()'
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/convexhull.cpp:41: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/convexhull.cpp:41: undefined reference to `cv::convexHull(cv::_InputArray const&, cv::_OutputArray const&, bool, bool)'
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/convexhull.cpp:43: undefined reference to `cv::Mat::operator=(cv::Scalar_<double> const&)'
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/convexhull.cpp:45: undefined reference to `cv::circle(cv::Mat&, cv::Point_<int>, int, cv::Scalar_<double> const&, int, int, int)'
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/convexhull.cpp:53: undefined reference to `cv::line(cv::Mat&, cv::Point_<int>, cv::Point_<int>, cv::Scalar_<double> const&, int, int, int)'
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/convexhull.cpp:57: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/convexhull.cpp:57: undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/convexhull.cpp:59: undefined reference to `cv::waitKey(int)'
build/Facedetect/MinGW-Windows/convexhull.o: In function `~Mat':
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/../../../../../CCompiler/OpenCV2.3/include/opencv2/core/mat.hpp:297: undefined reference to `cv::fastFree(void*)'
build/Facedetect/MinGW-Windows/convexhull.o:C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/../../../../../CCompiler/OpenCV2.3/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::create(int, int const*, int)'
make[2]: Leaving directory `/c/Users/Ryan/NetBeansProjects/Technabling/CppTutorials'
build/Facedetect/MinGW-Windows/convexhull.o:C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/../../../../../CCompiler/OpenCV2.3/include/opencv2/core/mat.hpp:381: undefined reference to `cv::Mat::deallocate()'
build/Facedetect/MinGW-Windows/convexhull.o: In function `Mat<cv::Point_<int> >':
make[1]: Leaving directory `/c/Users/Ryan/NetBeansProjects/Technabling/CppTutorials'
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/../../../../../CCompiler/OpenCV2.3/include/opencv2/core/mat.hpp:207: undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/../../../../../CCompiler/OpenCV2.3/include/opencv2/core/mat.hpp:207: undefined reference to `cv::Mat::copyTo(cv::_OutputArray const&) const'
build/Facedetect/MinGW-Windows/convexhull.o: In function `OutputArray<int>':
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/../../../../../CCompiler/OpenCV2.3/include/opencv2/core/mat.hpp:1110: undefined reference to `vtable for cv::_OutputArray'
build/Facedetect/MinGW-Windows/convexhull.o: In function `InputArray<int>':
C:\Users\Ryan\NetBeansProjects\Technabling\CppTutorials/../../../../../CCompiler/OpenCV2.3/include/opencv2/core/mat.hpp:1102: undefined reference to `vtable for cv::_InputArray'
collect2: ld returned 1 exit status
make[2]: *** [dist/Facedetect/MinGW-Windows/cpptutorials.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 2s)
 


Ctrl + clicking on theRNG() takes me into the core.hpp file at this little peice of code
1
2
//! returns the thread-local Random number generator
CV_EXPORTS RNG& theRNG();
and doing the same there on RNG& takes me to the class decleration in the same file and finally ctrl + click on the construtor declatation takes me into the operations.hpp file and thats where the chain ends.

I tried adding operations.hpp in my #include list but it made no difference.

So if anyone can tell me where I'm going wrong, how to solve it and / or how to avoid it in future it would be a great help.

Thanks for reading, I'll be back to check for replies frequently and will post up the solution should I find it in the mean time.

Ryan
Last edited on
Have you tried reversing the order of your namespace declarations? I don't "think" it should matter, but it would be a quick fix if it did!

Also double-check that the libraries are actually in the directory path your makefile specifies -- perhaps you have one to may or to few "../" in there.

Good luck!
Cheers for the reply, will try this out this week and let you know how I get on.
Nope, as you expected changing namespace declaration order made no difference.

The paths are auto-generated by NetBeans so should be fine.

I'm going to go back to the start of the guides and walk through setting up my environment again. I really can't see what it isn't finding the libraries I'm using, perhaps theRNG() isn't availble in MinGW, would that be a possibility?
Last edited on
I'm getting closer to narrowing down the problem.

After reading this useful post about the meaning of linking http://www.linuxforums.org/forum/programming-scripting/125296-g-returns-undefined-reference.html I think that there is a problem somewhere in the IDE explaining where the linked libraries are to the compiler. I can't imagin why because I have put in all the directories I can think of into the linker sections and also tried various combinations of pointing to the .dll files and the .lib files.

However looking at the output from the compiler I can see that it is using the -l arguement when compiling as in
 
 -L../../../../../CCompiler/OpenCV2.3/lib -lopencv_core230 -lopencv_highgui230 -lopencv_imgproc230 -lopencv_objdetect230 -lopencv_features2d230 

Which according to a post in the link will not work as -l only works with libraries starting libxxx. So maybe I am linking to the wrong files somehow although this doesn't make much sense since I am able to use other functions in opencv_core230, opencv_highgui230 and opencv_imgproc230 just fine.

I also noticed that is I change the code for the random number generator from
 
 RNG& rng = theRNG();

to
 
 RNG rng = RNG();

In another program that just prints random numbers to the screen then the program compiles and runs as expected.

Interestingly following the chain of RNG() takes me to core.hpp which has the prototype of the constructor and then following that takes me to operations.hpp which has the actual definition of it and all the methods. Whereas following theRNG() takes me to core.hpp for the prototype and then I can't follow it any further. For what I've learnt about C++ so far I think that this means the actual deffinition of theRNG() must be in one of the library files I'm trying to link to (.dll) but because the link isn't working this is why the undefined reference exception is being fired.

I just noticed that I neglected to say I'm working on a 64bit machine and I'm pretty certain the libraries I'm working with are 32. I've set the IDE to compile in 32bit so that shouldn't be an issue.
What errors are you getting now?
Topic archived. No new replies allowed.