LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Problem linking against OpenCV C++ API (https://www.linuxquestions.org/questions/linux-software-2/problem-linking-against-opencv-c-api-794485/)

Germanchu 03-10-2010 12:49 PM

Problem linking against OpenCV C++ API
 
Hi all,

I'm programming simple apps that work with OpenCV.
My problem is that when I compile any code that uses the cv::findContours(), cv::drawContours, cv::imwrite() or any of the functions that have a vector<> as parameter, the linker throws me an "undefined reference" error for the function.

I have checked eveything I can, such as:
1) Correct declaration of parameters and calling of the functions.
2) Checked that the functions are declared on the OpenCV headers and defined in the source code.
3) I even made an objdump of the .so files to see if the functions were compiled in.

This is one of the apps that are causing me trouble:

Code:

#include "cv.h"
#include "highgui.h"
#include <vector>

using namespace cv;

int main(int argc, char **argv) {
        Mat image;

        assert(argc == 2);

        image = imread(argv[1]);
        assert(image.data);

        Mat grayscale;
        cvtColor(image, grayscale, CV_RGB2GRAY);

        imshow("original", image);
        imshow("grayscale", grayscale);

        waitKey(0);

        std::vector<int> params;
        params.push_back(CV_IMWRITE_JPEG_QUALITY);
        params.push_back(100);

        string filename("image_up.jpg");
        imwrite(filename, grayscale, params);

        return 0;
        }

The error thrown is the following:

Code:

c++ -I/usr/include/opencv -lcv -lhighgui -o pic pic.cpp
/tmp/cctlqoGJ.o: In function `main':
pic.cpp:(.text+0x417): undefined reference to `cv::imwrite(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::Mat const&, std::vector<int, std::allocator<int> > const&)'
collect2: ld returned 1 exit status
make: *** [pic] Error 1

And the entry point in the library "libhighgui.so", to see that the function should be available:

Code:

...
00017850 g    DF .text        00000029  Base        cv::imwrite(std::string const&, cv::Mat const&, std::__cxx1998::vector<int, std::allocator<int> > const&)
...

I have apps with the other functions indicated that throw me a similar linking error. I'm lost here, and tried everything I know, so it would be great that you could give me a hint about this.

Thank you all ;-)

evo2 03-11-2010 10:19 PM

This works (compiles and links) fine for me. What is the output of:

Code:

readelf -Ws /usr/lib/libhighgui.so  | grep imwrite
Also are you sure it it linking against the file you think it is? Perhaps you have some other libhighgui.so somewhere.

Evo2.

Germanchu 03-16-2010 02:58 PM

Quote:

Originally Posted by evo2 (Post 3895201)
This works (compiles and links) fine for me. What is the output of:

Code:

readelf -Ws /usr/lib/libhighgui.so  | grep imwrite
Also are you sure it it linking against the file you think it is? Perhaps you have some other libhighgui.so somewhere.

Evo2.

Evo, thank you for your response, the output of that command is:

Code:

  833: 00017850    41 FUNC    GLOBAL DEFAULT  11 _ZN2cv7imwriteERKSsRKNS_3MatERKNSt9__cxx19986vectorIiSaIiEEE
Ask me if you need more data... I still could not solve this issue and i REALLY need to use the contour functions :(

Thank you very much!

Germanchu 03-16-2010 03:02 PM

Quote:

Originally Posted by evo2 (Post 3895201)
This works (compiles and links) fine for me. What is the output of:

Code:

readelf -Ws /usr/lib/libhighgui.so  | grep imwrite
Also are you sure it it linking against the file you think it is? Perhaps you have some other libhighgui.so somewhere.

Evo2.

I forgot to include in my previous post but I checked all the lib directories and I have no other copies of the OpenCV libs (other than those in /usr/lib).

Thanks and regards,

Germanchu 03-16-2010 03:04 PM

Evo, If you dont mind me asking, which version of OpenCV are you using? I'm on version 2.0.0-r1.. Thanks

Germanchu 03-16-2010 03:27 PM

The output of the command "nm -DC /usr/lib/libhighgui.so | grep imwrite" is:

Code:

00017850 T cv::imwrite(std::string const&, cv::Mat const&, std::__cxx1998::vector<int, std::allocator<int> > const&)
I dunno what is the std::__cxx1998::vector... stuff.. I assume it's a gcc notation for the 1998 corrections of the language.. I'm beggining to suspect that it has something to do with the vector parameters, as all of the functions that my apps do not link have some vector<> as parameter (findContours, imwrite). The strange thing is that I have no problem using split(Mat &, vector<Mat> &)..
I have no clue of why i'm having a problem only with some of the functions when the others work just fine..

mrgreybrown 03-22-2010 10:36 AM

did you ever figure out the problem?
I got the same one.

evo2 03-22-2010 08:06 PM

Quote:

Originally Posted by Germanchu (Post 3900832)
Evo, If you dont mind me asking, which version of OpenCV are you using? I'm on version 2.0.0-r1.. Thanks

I have the 2.0.0-4 version in Debian unstable. I'm not sure exactly what upstream version that corresponds to, since the changelog.gz refers to another log file which seems not to be included in the package.

Evo2.

Germanchu 03-23-2010 07:56 AM

Quote:

Originally Posted by mrgreybrown (Post 3907660)
did you ever figure out the problem?
I got the same one.

Nope.. I'm still trying to figure this out, I've posted like 3 messages about this in the OpenCV mailing list but got no answer :(.

Let me know if you figure this out..
Germán

Germanchu 04-28-2010 09:35 AM

The problem was solved using the latest version (haven't been released yet when I came up with this issue). With OpenCV version 2.1 (releases in April 2010), this doesn't happen to me anymore.

Thank you all for your help! ;)

Pinguinli 08-19-2010 06:36 AM

Same problem here with OpenCV 2.1
 
Hi there,

I think I am running into the same issue, but with OpenCV 2.1!
For me, using cv::Mat is not problem, but functions like cv::imread or cv::namedWindow cause the "undefined reference" error message.

And, surprisingly, all was running perfect as long as I was using the C API of OpenCV. Only when I started to use the C++ API, the problems started.

Has anybody an idea what could be the problem here? I am linking to cv210, highgui210, cvaux210 and cxcore210. Am I missing something here?

Thanks

evo2 08-19-2010 08:11 PM

Looks like the wrong libs are being found at runtime. Have you checked the output of ldd on the executable? Does it show the libs you expect?

Evo2.

Pinguinli 08-21-2010 06:32 AM

Hi Evo2,

Thanks for your hint. I thought that it is impossible that I link the wrong libraries, since it is the first time I am working with OpenCV and therefore have only one version istalled.

But you were right! I was linking the libraries in C:\OpenCV2.1\bin, and there were really some libraries with the correct name in this directory, but not the ones that I compiled myself (file creation timestamp was too old).
Finally I found that my compiled libs were going to C:\OpenCV2.1\CMakeFiles\bin (This depends on your actual CMake configuration!). Now linking to this directory, it works fine :-)


Also I hope I won't be banned now since I am working on windows...

Thanks, Pingulinli

Pinguinli 08-21-2010 08:24 AM

Hi again,

I was cheering too soon...
Although compilation and linking do not produce an error message anymore, I have now the following problem:
As soon as I try to open a named window (e.g. cv::namedWindow("Display", CV_WINDOW_AUTOSIZE);) the application crashes (Application has encountered a problem and needs to close...). But, surprisingly, the Window is opening with the correct name "Display" before the application crashes!

Other functions of the OpenCV library seem to work, e.g. creating a cv::Mat and then loading an image to it by imread(argv[1], 1);

Is this another linker problem? Unfortunately when debugging in Eclipse, I can only see that it crashes at the namedWindow function call, but I cannot jump into it (but anyway I am not sure if that would be of any help to me...)

Regards, Pingulinli

PS.
Here is what Dependency Walker outputs when profiling the crashing application:
[...]
GetProcAddress(0x75250000 [MSCTFIME.IME], "ImeGetImeMenuItems") called from "IMM32.DLL" at address 0x76337354 and returned NULL. Error: The procedure was not found (127).
GetProcAddress(0x75250000 [MSCTFIME.IME], "CtfImeInquireExW") called from "IMM32.DLL" at address 0x76337394 and returned 0x75269548.
GetProcAddress(0x75250000 [MSCTFIME.IME], "CtfImeSelectEx") called from "IMM32.DLL" at address 0x763373A8 and returned 0x752692C5.
GetProcAddress(0x75250000 [MSCTFIME.IME], "CtfImeEscapeEx") called from "IMM32.DLL" at address 0x763373BC and returned 0x752696FE.
GetProcAddress(0x75250000 [MSCTFIME.IME], "CtfImeGetGuidAtom") called from "IMM32.DLL" at address 0x763373D0 and returned 0x75269737.
GetProcAddress(0x75250000 [MSCTFIME.IME], "CtfImeIsGuidMapEnable") called from "IMM32.DLL" at address 0x763373E4 and returned 0x7526931A.
Second chance exception 0xC0000005 (Access Violation) occurred in "LIBHIGHGUI210.DLL" at address 0x6A3350C4.
Exited "TESTE.EXE" (process 0x98C) with code -1073741819 (0xC0000005).

Pinguinli 08-22-2010 07:01 AM

Hi

I think I have now finally a working OpenCV environment (after hours and hours of trying all kind of stuff...)
In short, the solution was to use the gcc version 4.4.1, which I downloaded here:
http://sourceforge.net/downloads/tdm...vious/1.908.0/
Download tdm-mingw-1.908.0-4.4.1-2.exe and install it. Do not use the web installer since this will download the latest version (which gave me the troubles described above).
Then compile OpenCV 2.1 as described in the instructions, using CMake etc.

After this I am now able to use the C interface (e.g. cvLoadImage) as well as the C++ interface (e.g. cv::imread).

When I tried to use the windows binarys (dll's) of the OpenCV library which can be downloaded from the OpenCV website (OpenCV-2.1.0-win32-vs2008.exe), I can use the C interface but not the C++ interface. Using the C++ interface will give a Linker error as described in my previous posts. I assume this is due to some incompatible name mangling for the C++ functions in the dlls.

When I compiled OpenCV 2.1 with TDM-GCC 4.5, everything was compiling and linking fine, but the cv::namedWindow crashed everytime it was called! I assume this is a bug in OpenCV which is only visible with this new compiler.

I hope this helps some people so they do not have to go through all the frustration that I went through!

BTW, I am working with Eclipse Helios with CDT plugin, on Windows XP SP2.

Best Regards,
Pinguinli


All times are GMT -5. The time now is 11:38 AM.