未定义库的引用,那么如何找到正确的路径? [英] Undefine reference for libraries, so How could I find the right path?
问题描述
我正在尝试在Ubuntu中编译v4l2示例,但出现以下错误:
I am trying to compile a v4l2 example in Ubuntu but I am getting the 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 to `XDestroyImage'
viewer.c:(.text+0x256): undefined reference to `XFreeGC'
viewer.c:(.text+0x277): undefined reference to `XShmDetach'
viewer.c:(.text+0x2ac): undefined reference to `XFreePixmap'
/tmp/ccUjnjWQ.o: In function `image_create':
viewer.c:(.text+0x305): undefined reference to `XCreateGC'
viewer.c:(.text+0x31d): undefined reference to `XGetWindowAttributes'
viewer.c:(.text+0x39e): undefined reference to `XShmCreateImage'
viewer.c:(.text+0x3f5): undefined reference to `XShmAttach'
viewer.c:(.text+0x44e): undefined reference to `XCreateImage'
viewer.c:(.text+0x494): undefined reference to `XShmQueryExtension'
viewer.c:(.text+0x4b4): undefined reference to `XShmPixmapFormat'
viewer.c:(.text+0x4dc): undefined reference to `XShmCreatePixmap'
/tmp/ccUjnjWQ.o: In function `image_put':
viewer.c:(.text+0x54c): undefined reference to `XPutImage'
viewer.c:(.text+0x586): undefined reference to `XShmPutImage'
/tmp/ccUjnjWQ.o: In function `main':
viewer.c:(.text.startup+0x18b): undefined reference to `XOpenDisplay'
viewer.c:(.text.startup+0x1b1): undefined reference to `XScreenOfDisplay'
viewer.c:(.text.startup+0x1ee): undefined reference to `XCreateSimpleWindow'
viewer.c:(.text.startup+0x249): undefined reference to `XMapRaised'
viewer.c:(.text.startup+0x263): undefined reference to `XStoreName'
viewer.c:(.text.startup+0x280): undefined reference to `XGetWindowAttributes'
viewer.c:(.text.startup+0x92f): undefined reference to `XPending'
viewer.c:(.text.startup+0x94c): undefined reference to `XNextEvent'
viewer.c:(.text.startup+0xaee): undefined reference to `XPending'
viewer.c:(.text.startup+0xb0b): undefined reference to `XNextEvent'
viewer.c:(.text.startup+0xf39): undefined reference to `XPending'
viewer.c:(.text.startup+0xf56): undefined reference to `XNextEvent'
collect2: error: ld returned 1 exit status
make: *** [viewer] Error 1
我看到的是-lx11和-lXext的路径不是-L/usr/include.如何找到这些库的正确路径?
What I can see is that the path for -lx11 and -lXext isn't -L/usr/include. How can I find the right path for those libraries?
谢谢.
推荐答案
顺序是错误的,您需要将 -lX11 -lXext
放在源代码/之后对象文件.
as Chris has pointed out, the order is wrong, you need to put the -lX11 -lXext
after the source-code/object-files.
这是因为现代编译器会尝试优化最终结果,而不是针对未使用的库进行链接.他们通过维护对象中未解析符号的列表来做到这一点,并使用链接器参数中出现的所有二进制二进制文件来解析这些符号.
this is because modern compilers try to optimize the final result and not link against unused libraries. they do so by maintaining a list of unresolved symbols within an object and use any binary files that come aferwards in the linker arguments to resolve those symbols.
您的程序 test 使用 libfoo 中的函数 do_foo()
和< libbar .
your program test uses the function do_foo()
from libfoo and the function do_bar_do()
from libbar.
您使用以下链接:
$ gcc -o test test.o -lfoo -lbar
链接器首先搜索 test.o
,并注意到某些符号( do_foo
和 do_bar_do
)未在任何地方定义.然后进入 libfoo
(在 test.o
之后指定),并发现它提供了 do_foo
,因此它会创建代码以从您的程序. do_bar_do
仍未解决,直到链接程序检查 libbar
.
the linker first searches test.o
and notices that some symbols (do_foo
and do_bar_do
) are not defined anywhere. it then proceeds to libfoo
(specified right after test.o
) and finds that it provides do_foo
, so it creates code to use it from your program. do_bar_do
is still unresolved, until the linker checks upon libbar
.
考虑以错误的方式进行操作:
consider doing it the wrong way:
$ gcc -o test -lfoo test.o -lbar
链接器将首先检查 libfoo
并查看它是否不包含任何未解析的符号.凉爽的.然后它将继续进行 test.o
并注意 do_bar_do
和 do_foo
. do_bar_do
由右侧的 libbar
解决,但 do_foo
却根本没有解决,并且会出现错误:
the linker will first check libfoo
and see that it doesn't contain any unresolved symbols. cool. it will then proceed to test.o
and notice do_bar_do
and do_foo
. do_bar_do
is resolved by the right-hand libbar
but do_foo
is not resolved at all, and you get an error:
对`do_foo'的未定义引用
undefined reference to `do_foo'
但是代码本来应该是一个教程..."
那为什么不起作用?
"but the code is meant to be a tutorial..."
so why is it not working?
较旧的编译器,其中对依赖项的顺序有些松懈(它们将检查所有二进制文件/库/对象是否可以解析给定的符号);这就是为什么您仍然可以在其中找到将库链接到对象文件之前的代码的原因.
older compilers where a bit lax about the order of dependencies (they would check all binaries/libraries/objects whether a given symbol could be resolved); that's why you can still find code out there that puts the libraries to link against before the object files.
这篇关于未定义库的引用,那么如何找到正确的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!