LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2015, 07:04 AM   #1
savio
LQ Newbie
 
Registered: May 2015
Posts: 29

Rep: Reputation: Disabled
using gcc to link libraries and to include headers


Hi,

I am writing a program using opencv calls and I want to compile it and make the executable.
I already read the gcc tutorial but I would like to know how to link opencv libraries I am using.
My current command to compile is the following:

Code:
gcc -I/home/myname/opencv-3.0.0/include/opencv -L/home/myname/opencv-3.0.0/cmake_binary_dir/lib -lopencv_core -lopencv_highgui -lopencv_imgproc program.c -o program
It doesn't work.
What is wrong with that?

Thanks in advance for help
 
Old 07-16-2015, 07:16 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
What does "It doesn't work" mean?
Any error messages?
What is your distribution and gcc version?
 
Old 07-16-2015, 08:08 AM   #3
savio
LQ Newbie
 
Registered: May 2015
Posts: 29

Original Poster
Rep: Reputation: Disabled
I tried in two ways:
Code:
savio@savio-R580:~/programmi$ gcc -I/home/savio/opencv-3.0.0/include/opencv -L/home/savio/opencv-3.0.0/cmake_binary_dir/lib -llibopencv_core -llibopencv_highgui -llibopencv_imgproc imagefilter.c -o imagefilter
/usr/bin/ld: impossibile trovare -llibopencv_core
/usr/bin/ld: impossibile trovare -llibopencv_highgui
/usr/bin/ld: impossibile trovare -llibopencv_imgproc
collect2: error: ld returned 1 exit status
and
Code:
savio@savio-R580:~/programmi$ gcc -I/home/savio/opencv-3.0.0/include/opencv -L/home/savio/opencv-3.0.0/cmake_binary_dir/lib -lopencv_core -lopencv_highgui -lopencv_imgproc imagefilter.c -o imagefilter
/tmp/ccDUEgAk.o: nella funzione "cvDecRefData":
imagefilter.c:(.text+0xa6a): riferimento non definito a "cvFree_"
imagefilter.c:(.text+0xaf2): riferimento non definito a "cvFree_"
/tmp/ccDUEgAk.o: nella funzione "cvGetRow":
imagefilter.c:(.text+0xc08): riferimento non definito a "cvGetRows"
/tmp/ccDUEgAk.o: nella funzione "cvGetCol":
imagefilter.c:(.text+0xc36): riferimento non definito a "cvGetCols"
/tmp/ccDUEgAk.o: nella funzione "cvReleaseMatND":
imagefilter.c:(.text+0xc50): riferimento non definito a "cvReleaseMat"
/tmp/ccDUEgAk.o: nella funzione "cvSubS":
imagefilter.c:(.text+0xdac): riferimento non definito a "cvAddS"
/tmp/ccDUEgAk.o: nella funzione "cvCloneSeq":
imagefilter.c:(.text+0xde5): riferimento non definito a "cvSeqSlice"
/tmp/ccDUEgAk.o: nella funzione "cvSetNew":
imagefilter.c:(.text+0xe52): riferimento non definito a "cvSetAdd"
/tmp/ccDUEgAk.o: nella funzione "cvGetSetElem":
imagefilter.c:(.text+0xefa): riferimento non definito a "cvGetSeqElem"
/tmp/ccDUEgAk.o: nella funzione "cvReadIntByName":
imagefilter.c:(.text+0xfa9): riferimento non definito a "cvGetFileNodeByName"
/tmp/ccDUEgAk.o: nella funzione "cvReadRealByName":
imagefilter.c:(.text+0x1053): riferimento non definito a "cvGetFileNodeByName"
/tmp/ccDUEgAk.o: nella funzione "cvReadStringByName":
imagefilter.c:(.text+0x10e6): riferimento non definito a "cvGetFileNodeByName"
/tmp/ccDUEgAk.o: nella funzione "cvReadByName":
imagefilter.c:(.text+0x1126): riferimento non definito a "cvGetFileNodeByName"
imagefilter.c:(.text+0x113c): riferimento non definito a "cvRead"
/tmp/ccDUEgAk.o: nella funzione "cvContourPerimeter":
imagefilter.c:(.text+0x1170): riferimento non definito a "cvArcLength"
/tmp/ccDUEgAk.o: nella funzione "cvCalcHist":
imagefilter.c:(.text+0x11b2): riferimento non definito a "cvCalcArrHist"
/tmp/ccDUEgAk.o: nella funzione "cvEllipseBox":
imagefilter.c:(.text+0x127e): riferimento non definito a "cvEllipse"
/tmp/ccDUEgAk.o: nella funzione "cvFont":
imagefilter.c:(.text+0x12c8): riferimento non definito a "cvInitFont"
/tmp/ccDUEgAk.o: nella funzione "main":
imagefilter.c:(.text+0x1396): riferimento non definito a "cvLoadImage"
imagefilter.c:(.text+0x13af): riferimento non definito a "cvGetSize"
imagefilter.c:(.text+0x13c1): riferimento non definito a "cvCreateImage"
imagefilter.c:(.text+0x13ed): riferimento non definito a "cvSmooth"
imagefilter.c:(.text+0x1403): riferimento non definito a "cvSaveImage"
imagefilter.c:(.text+0x141b): riferimento non definito a "cvReleaseImage"
imagefilter.c:(.text+0x1427): riferimento non definito a "cvReleaseImage"
collect2: error: ld returned 1 exit status
How sould I correctly specify the libraries?
 
Old 07-16-2015, 08:08 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You don't have to save line-breaks, use them generously:

Code:
gcc -I/home/myname/opencv-3.0.0/include/opencv \
    -L/home/myname/opencv-3.0.0/cmake_binary_dir/lib \
    -lopencv_core -lopencv_highgui -lopencv_imgproc program.c -o program
That 'cmake_binary_dir' doesn't sound plausible... Try this:

Code:
find /home/myname/opencv-3.0.0 -name 'libopencv_core*'
PS: unset LC_ALL in this moment. Translated error messages are unusable!

Last edited by NevemTeve; 07-16-2015 at 08:09 AM.
 
Old 07-16-2015, 08:10 AM   #5
savio
LQ Newbie
 
Registered: May 2015
Posts: 29

Original Poster
Rep: Reputation: Disabled
I compiled and installed opencv sources by myself, specifying that directory:
Code:
savio@savio-R580:~/programmi$ find /home/savio/opencv-3.0.0 -iname 'libopencv_core*'
/home/savio/opencv-3.0.0/cmake_binary_dir/lib/libopencv_core.so.3.0
/home/savio/opencv-3.0.0/cmake_binary_dir/lib/libopencv_core.so
/home/savio/opencv-3.0.0/cmake_binary_dir/lib/libopencv_core_pch_dephelp.a
/home/savio/opencv-3.0.0/cmake_binary_dir/lib/libopencv_core.so.3.0.0

Last edited by savio; 07-16-2015 at 08:16 AM.
 
Old 07-16-2015, 08:14 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
In your case it is:

Code:
find /home/savio/opencv-3.0.0 -iname 'libopencv_core*'
 
Old 07-16-2015, 08:17 AM   #7
savio
LQ Newbie
 
Registered: May 2015
Posts: 29

Original Poster
Rep: Reputation: Disabled
Code:
savio@savio-R580:~/programmi$ find /home/savio/opencv-3.0.0 -name 'libopencv_core*'
/home/savio/opencv-3.0.0/cmake_binary_dir/lib/libopencv_core.so.3.0
/home/savio/opencv-3.0.0/cmake_binary_dir/lib/libopencv_core.so
/home/savio/opencv-3.0.0/cmake_binary_dir/lib/libopencv_core_pch_dephelp.a
/home/savio/opencv-3.0.0/cmake_binary_dir/lib/libopencv_core.so.3.0.0
 
Old 07-16-2015, 08:29 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
(Just for fun I tried to download this piece of shi^H^H^H software, but it is bigger that the linux kernel..)
 
Old 07-16-2015, 08:51 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Also, the recent versions of gcc in some distros seem to have stricter rules concerning the order in which the objects are specified to the linker. The order of the objects should correspond to the way they depend on each other.
So, imagefilter.c should go first, before all the libraries because it uses symbols defined in those libraries, and so on. I don't know how the so files from OpenCV depend on each other (use ldd to find out), but an object that uses something should always go before the object that provides it.
 
Old 07-16-2015, 09:02 AM   #10
savio
LQ Newbie
 
Registered: May 2015
Posts: 29

Original Poster
Rep: Reputation: Disabled
It is a bit better but still it doesn't work:
Code:
savio@savio-R580:~/programmi$ gcc imagefilter.c -o imagefilter -I/home/savio/opencv-3.0.0/include/opencv -L/home/savio/opencv-3.0.0/cmake_binary_dir/lib -lopencv_core -lopencv_highgui -lopencv_imgproc
/tmp/ccmZzYQz.o: nella funzione "main":
imagefilter.c:(.text+0x1396): riferimento non definito a "cvLoadImage"
imagefilter.c:(.text+0x1403): riferimento non definito a "cvSaveImage"
collect2: error: ld returned 1 exit status
 
Old 07-16-2015, 09:09 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
If you're lucky, you have a *.pc file somewhere:

Code:
find ~/opencv-3.0.0 -name '*.pc'
 
Old 07-16-2015, 09:13 AM   #12
savio
LQ Newbie
 
Registered: May 2015
Posts: 29

Original Poster
Rep: Reputation: Disabled
I found dependencies using ldd command.
Now it compiles but not corretly:
Code:
savio@savio-R580:~/programmi$ gcc imagefilter.c -o imagefilter -I/home/savio/opencv-3.0.0/include/opencv -L/home/savio/opencv-3.0.0/cmake_binary_dir/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -lopencv_highgui
savio@savio-R580:~/programmi$ ./imagefilter lena_noise.BMP 
./imagefilter: error while loading shared libraries: libopencv_imgcodecs.so.3.0: cannot open shared object file: No such file or directory
There is the .pc file:
Code:
savio@savio-R580:~/programmi$ find ~/opencv-3.0.0 -name '*.pc'
/home/savio/opencv-3.0.0/cmake_binary_dir/unix-install/opencv.pc
Anyway, looking at opencv.pc file, I tried also modifying the gcc command parameters like this:
Code:
savio@savio-R580:~/programmi$ gcc imagefilter.c -o imagefilter -I/usr/local/include -L/usr/local/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_highgui -lopencv_core
savio@savio-R580:~/programmi$ ./imagefilter lena_noise.BMP 
./imagefilter: error while loading shared libraries: libopencv_imgcodecs.so.3.0: cannot open shared object file: No such file or directory
but I have exactly the same problem.

Last edited by savio; 07-16-2015 at 09:27 AM.
 
Old 07-16-2015, 10:09 AM   #13
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Try running the program with

Code:
LD_LIBRARY_PATH=/home/savio/opencv-3.0.0/cmake_binary_dir/lib:$LD_LIBRARY_PATH ./imagefilter lena_noise.BMP
 
Old 07-16-2015, 10:13 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Cool!

So try this:
Code:
export PKG_CONFIG_PATH="/home/savio/opencv-3.0.0/cmake_binary_dir/unix-install:$PKG_CONFIG_PATH"

gcc $(pkg-config --cflags opencv) imagefilter.c \
    -o imagefilter $(pkg-config --libs opencv)

Last edited by NevemTeve; 07-16-2015 at 10:14 AM.
 
  


Reply



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
[SOLVED] Recursively include headers in include option for gcc kvm1983 Linux - Newbie 5 09-04-2012 03:21 PM
include kernel headers rrlangly Linux - Kernel 5 04-14-2012 05:02 PM
which headers usr/include/ or /usr/include/nptl/? Chris.Rector Linux - Enterprise 0 02-02-2007 01:40 PM
Unable to include headers in Makefile Raphexion Programming 2 11-05-2005 08:00 AM
How to add boost libraries to gcc include list kiku Linux - Newbie 1 03-04-2005 02:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:53 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration