LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-10-2010, 12:49 PM   #1
Germanchu
LQ Newbie
 
Registered: Nov 2009
Location: Buenos Aires, Argentina
Distribution: Gentoo
Posts: 23

Rep: Reputation: 0
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 ;-)
 
Old 03-11-2010, 10:19 PM   #2
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Debian, SL
Posts: 5,103

Rep: Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102
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.
 
Old 03-16-2010, 02:58 PM   #3
Germanchu
LQ Newbie
 
Registered: Nov 2009
Location: Buenos Aires, Argentina
Distribution: Gentoo
Posts: 23

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by evo2 View Post
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!
 
Old 03-16-2010, 03:02 PM   #4
Germanchu
LQ Newbie
 
Registered: Nov 2009
Location: Buenos Aires, Argentina
Distribution: Gentoo
Posts: 23

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by evo2 View Post
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,
 
Old 03-16-2010, 03:04 PM   #5
Germanchu
LQ Newbie
 
Registered: Nov 2009
Location: Buenos Aires, Argentina
Distribution: Gentoo
Posts: 23

Original Poster
Rep: Reputation: 0
Evo, If you dont mind me asking, which version of OpenCV are you using? I'm on version 2.0.0-r1.. Thanks
 
Old 03-16-2010, 03:27 PM   #6
Germanchu
LQ Newbie
 
Registered: Nov 2009
Location: Buenos Aires, Argentina
Distribution: Gentoo
Posts: 23

Original Poster
Rep: Reputation: 0
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..
 
Old 03-22-2010, 10:36 AM   #7
mrgreybrown
LQ Newbie
 
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
did you ever figure out the problem?
I got the same one.
 
Old 03-22-2010, 08:06 PM   #8
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Debian, SL
Posts: 5,103

Rep: Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102
Quote:
Originally Posted by Germanchu View Post
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.
 
Old 03-23-2010, 07:56 AM   #9
Germanchu
LQ Newbie
 
Registered: Nov 2009
Location: Buenos Aires, Argentina
Distribution: Gentoo
Posts: 23

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by mrgreybrown View Post
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
 
Old 04-28-2010, 09:35 AM   #10
Germanchu
LQ Newbie
 
Registered: Nov 2009
Location: Buenos Aires, Argentina
Distribution: Gentoo
Posts: 23

Original Poster
Rep: Reputation: 0
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!
 
Old 08-19-2010, 06:36 AM   #11
Pinguinli
LQ Newbie
 
Registered: Aug 2010
Posts: 4

Rep: Reputation: 0
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
 
Old 08-19-2010, 08:11 PM   #12
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Debian, SL
Posts: 5,103

Rep: Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102Reputation: 1102
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.
 
Old 08-21-2010, 06:32 AM   #13
Pinguinli
LQ Newbie
 
Registered: Aug 2010
Posts: 4

Rep: Reputation: 0
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
 
Old 08-21-2010, 08:24 AM   #14
Pinguinli
LQ Newbie
 
Registered: Aug 2010
Posts: 4

Rep: Reputation: 0
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).
 
Old 08-22-2010, 07:01 AM   #15
Pinguinli
LQ Newbie
 
Registered: Aug 2010
Posts: 4

Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with the Networking API vijaya_svk Programming 2 08-02-2009 01:18 PM
OpenCV compilation problem (add directory for including header files) maikki Programming 2 01-07-2009 08:07 AM
MySQL C API problem anroy Programming 6 12-07-2006 08:23 PM
Problem in using both load time linking and runtime linking durgaprasad_j Linux - General 0 08-01-2006 03:49 AM
linking C api for mysql toships Programming 3 04-08-2004 11:29 PM


All times are GMT -5. The time now is 05:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration