Problem using --as-needed during linking (Bug #2110)
Description
Given the sample file:
#include <opencv/cxcore.h>
long check_cvCreateImageHeader(void) { return (long) cvCreateImageHeader; }
int main(void) { return 0; }
I can compile it without problems:
gcc -Wl,--as-needed `pkg-config opencv --cflags` -c check.c
If I try and link it using the command:
gcc -Wl,--as-needed `pkg-config opencv --libs` check.o -o check.out
I get the following errors as output:
check.o: In function `cvDecRefData':
check.c:(.text+0xb06): undefined reference to `cvFree_'
check.c:(.text+0xb8e): undefined reference to `cvFree_'
check.o: In function `cvGetRow':
check.c:(.text+0xca9): undefined reference to `cvGetRows'
check.o: In function `cvGetCol':
check.c:(.text+0xce0): undefined reference to `cvGetCols'
check.o: In function `cvReleaseMatND':
check.c:(.text+0xcff): undefined reference to `cvReleaseMat'
check.o: In function `cvSubS':
check.c:(.text+0xe5c): undefined reference to `cvAddS'
check.o: In function `cvCloneSeq':
check.c:(.text+0xe9e): undefined reference to `cvSeqSlice'
check.o: In function `cvSetNew':
check.c:(.text+0xf10): undefined reference to `cvSetAdd'
check.o: In function `cvGetSetElem':
check.c:(.text+0xfb9): undefined reference to `cvGetSeqElem'
check.o: In function `cvEllipseBox':
check.c:(.text+0x10a7): undefined reference to `cvEllipse'
check.o: In function `cvFont':
check.c:(.text+0x10e9): undefined reference to `cvInitFont'
check.o: In function `cvReadIntByName':
check.c:(.text+0x11d7): undefined reference to `cvGetFileNodeByName'
check.o: In function `cvReadRealByName':
check.c:(.text+0x128a): undefined reference to `cvGetFileNodeByName'
check.o: In function `cvReadStringByName':
check.c:(.text+0x1312): undefined reference to `cvGetFileNodeByName'
check.o: In function `cvReadByName':
check.c:(.text+0x1352): undefined reference to `cvGetFileNodeByName'
check.c:(.text+0x1368): undefined reference to `cvRead'
check.o: In function `check_cvCreateImageHeader':
check.c:(.text+0x1373): undefined reference to `cvCreateImageHeader'
collect2: ld returned 1 exit status
If I omit -Wl,--as-needed, then linking succeeds.
pkg-config is working fine:
mpenkov@misha-desktop:~/Desktop$ pkg-config --libs --cflags opencv
-I/usr/local/include/opencv -I/usr/local/include /usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_contrib.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_gpu.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_legacy.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_nonfree.so /usr/local/lib/libopencv_objdetect.so /usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_ts.so /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videostab.so
The reason this is a problem is I'm trying to build ffmpeg with opencv support (it uses opencv for some filtering), and it's configure script falls over the above error trying to detect opencv.
I've been having a conversation with one of the ffmpeg developers, he has an opinion that this is an opencv issue. The link to the conversation on Gmane is below:
http://comments.gmane.org/gmane.comp.video.ffmpeg.user/38042
I'm using OpenCV version 2.4.1 on Linux x64 (Ubuntu 10.04 LTS) with gcc 4.4.3.
History
Updated by Andrey Kamaev 11 months ago
I'm able to compile your sample with the following command:
gcc -Wl,--as-needed check.o `pkg-config opencv --libs` -o check.out
- Status changed from Open to Cancelled
- Target version set to 2.4.2
- Assignee changed from Alexander Shishkov to Andrey Kamaev
Updated by Michael Penkov 11 months ago
Any ideas why it wouldn't be working on my side?
I built from source as described in the Installation Guide (http://opencv.willowgarage.com/wiki/InstallGuide).
Updated by Jukka Lankinen 8 months ago
I had similar experience with a debian-based system. The problem was related to this:
http://wiki.debian.org/ToolChain/DSOLinking
This makes the order of the linking important. So if you're using Cmake to compile and use OpenCV, the order of the libraries needs to be changed.
My linker parameters:
SET (CMAKE_EXE_LINKER_FLAGS "-s -Wl,--no-add-needed -Wl,--as-needed")
And the component order (in my case):
FIND_PACKAGE(OpenCV COMPONENTS highgui nonfree features2d imgproc flann core video)
Hopefully this gives some insight. This might be a bug (minor) in OpenCV cmake generation.