linux - Undefine reference for libraries, so How could I find the right path? -
i trying compile v4l2 example in ubuntu getting following error:
guilherme@notedev01:~/downloads/v4l2_samples-0.4.1$ make gcc -o2 -l/usr/include -lx11 -lxext -o viewer viewer.c /tmp/ccujnjwq.o: in function `image_destroy': viewer.c:(.text+0x234): undefined reference `xdestroyimage' viewer.c:(.text+0x256): undefined reference `xfreegc' viewer.c:(.text+0x277): undefined reference `xshmdetach' viewer.c:(.text+0x2ac): undefined reference `xfreepixmap' /tmp/ccujnjwq.o: in function `image_create': viewer.c:(.text+0x305): undefined reference `xcreategc' viewer.c:(.text+0x31d): undefined reference `xgetwindowattributes' viewer.c:(.text+0x39e): undefined reference `xshmcreateimage' viewer.c:(.text+0x3f5): undefined reference `xshmattach' viewer.c:(.text+0x44e): undefined reference `xcreateimage' viewer.c:(.text+0x494): undefined reference `xshmqueryextension' viewer.c:(.text+0x4b4): undefined reference `xshmpixmapformat' viewer.c:(.text+0x4dc): undefined reference `xshmcreatepixmap' /tmp/ccujnjwq.o: in function `image_put': viewer.c:(.text+0x54c): undefined reference `xputimage' viewer.c:(.text+0x586): undefined reference `xshmputimage' /tmp/ccujnjwq.o: in function `main': viewer.c:(.text.startup+0x18b): undefined reference `xopendisplay' viewer.c:(.text.startup+0x1b1): undefined reference `xscreenofdisplay' viewer.c:(.text.startup+0x1ee): undefined reference `xcreatesimplewindow' viewer.c:(.text.startup+0x249): undefined reference `xmapraised' viewer.c:(.text.startup+0x263): undefined reference `xstorename' viewer.c:(.text.startup+0x280): undefined reference `xgetwindowattributes' viewer.c:(.text.startup+0x92f): undefined reference `xpending' viewer.c:(.text.startup+0x94c): undefined reference `xnextevent' viewer.c:(.text.startup+0xaee): undefined reference `xpending' viewer.c:(.text.startup+0xb0b): undefined reference `xnextevent' viewer.c:(.text.startup+0xf39): undefined reference `xpending' viewer.c:(.text.startup+0xf56): undefined reference `xnextevent' collect2: error: ld returned 1 exit status make: *** [viewer] error 1
what can see path -lx11 , -lxext isn't -l/usr/include. how can find right path libraries?
thanks.
as chris has pointed out, order wrong, need put -lx11 -lxext
after source-code/object-files.
this because modern compilers try optimize final result , not link against unused libraries. maintaining list of unresolved symbols within object , use binary files come aferwards in linker arguments resolve symbols.
example
your program test uses function do_foo()
libfoo , function do_bar_do()
libbar.
you link using:
$ gcc -o test test.o -lfoo -lbar
the linker first searches test.o
, notices symbols (do_foo
, do_bar_do
) not defined anywhere. proceeds libfoo
(specified right after test.o
) , finds provides do_foo
, creates code use program. do_bar_do
still unresolved, until linker checks upon libbar
.
consider doing wrong way:
$ gcc -o test -lfoo test.o -lbar
the linker first check libfoo
, see doesn't contain unresolved symbols. cool. proceed test.o
, notice do_bar_do
, do_foo
. do_bar_do
resolved right-hand libbar
do_foo
not resolved @ all, , error:
undefined reference `do_foo'
"but code meant tutorial..."
so why not working?
older compilers bit lax order of dependencies (they check binaries/libraries/objects whether given symbol resolved); that's why can still find code out there puts libraries link against before object files.
Comments
Post a Comment