简体   繁体   English

“警告:将共享库与静态库链接起来不可移植”是什么意思?

[英]What is the meaning of “Warning: Linking the shared library against static library is not portable”?

I am making one dynamic library by using some function of libmxml.a library but I get this warning:我正在使用 libmxml.a 库的某些功能制作一个动态库,但收到此警告:

*Warning: Linking the shared library libgstmatroskademux.la against the _
*static library /home/Mr32/gst-template4_final/gst-plugin/src/libmxml.a _
is not portable!

I also get this warning:我也收到此警告:

gcc: /home/Mr32/gst-template4_final/gst-plugin/src/libmxml.a: linker _
input file unused because linking not done

So what's the meaning of this warning and how could I solve it?那么这个警告的含义是什么,我该如何解决呢?

Edit :编辑 :

There is one already autogenerated make file for compiling the gstreamer plugin.已经有一个用于编译 gstreamer 插件的自动生成的 make 文件。 Now to use some function of libmxml.a in that plugin I have added $(PATH)/libmxml.a in the GST_CFLAGS variable in the make file.现在要在该插件中使用 libmxml.a 的某些功能,我在 make 文件的GST_CFLAGS变量中添加了$(PATH)/libmxml.a Now, when I do make and make install , the plugin works fine, but I still get this warning.现在,当我执行makemake install ,插件工作正常,但我仍然收到此警告。

Ensure that object files in libmxml.a were built with -fPIC .确保libmxml.a中的目标文件是使用-fPIC构建的。 It's necessary to build a shared library.有必要建立一个共享库。 See also http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html另见http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

Here's a quick example这是一个快速示例

$ cat stat.c 
int five() { return 5; }
$ gcc -c stat.c -fPIC
$ ar crus libstat.a stat.o
$ cat dynamic.c
int ten() { return five() + five(); }
$ gcc -c dynamic.c -fPIC
$ gcc -shared -o libdyn.so dynamic.o -L. -lstat
$ ldd libdyn.so # Just to show static linkage to libstat.a
  linux-vdso.so.1 =>  (0x00007fffca1b8000)
  libc.so.6 => /lib/libc.so.6 (0x00007fc004649000)
  /lib/ld-linux-x86-64.so.2 (0x00007fc004bf7000)
$ cat main.c 
int main() { return ten(); }
$ gcc main.c -L. -ldyn
$ LD_LIBRARY_PATH=. ./a.out 
$ echo $?
10

Linking shared libraries to static libraries is not possible (unless you really know very well what you are doing).将共享库链接到静态库是不可能的(除非您非常清楚自己在做什么)。 Don't do it.不要这样做。

The first warning is from libtool.第一个警告来自 libtool。 It tells you, that the operation you asked for will do different things on different systems and some of those things are probably not what you want.它告诉你,你要求的操作会在不同的系统上做不同的事情,其中​​一些事情可能不是你想要的。 Often it's just going to fail in various spectacular ways, because code that goes in shared and static libraries needs to be compiled with different compiler flags.通常它只会以各种惊人的方式失败,因为进入共享和静态库的代码需要使用不同的编译器标志进行编译。

The second warning is from gcc.第二个警告来自 gcc。 It is telling you that providing static library when compiling is pointless.它告诉你编译时提供静态库是没有意义的。 That's because you have $(PATH)/libmxml.a in C FLAGS , where it has no business of being.那是因为您在C FLAGS$(PATH)/libmxml.a ,它与存在无关。 In fact, most of the time you should not have $(PATH)/libmxml.a , but -L$(PATH) -lmxml instead.事实上,大多数的时候,你应该有$(PATH)/libmxml.a ,但-L$(PATH) -lmxml代替。 That should still go in LD FLAGS , but gcc won't complain if this makes it to the compiler command-line too.那应该仍然在LD FLAGS ,但是如果这也进入编译器命令行,gcc 不会抱怨。

Linking the shared library libgstmatroskademux.la against the static library将共享库 libgstmatroskademux.la 链接到静态库

This is warning you that if you eg tried to build this on 64-bit Linux, it would likely fail.这是警告您,例如,如果您尝试在 64 位 Linux 上构建它,它可能会失败。 That's because on x86_64, all code that gets linked into a shared library must be compiled with -fPIC flag, and code that lives in .a libraries usually isn't.这是因为在 x86_64 上,所有链接到共享库的代码都必须使用-fPIC标志进行编译,而位于.a库中的代码通常不是。

gcc: .../libmxml.a: linker input file unused because linking not done gcc: .../libmxml.a: 链接器输入文件未使用,因为链接未完成

This is warning you that you have a bogus command line.这是警告你你有一个虚假的命令行。 Most likely you are compiling something, and have -c on the command line (which tells GCC to stop after compiling source, and not perform linking).很可能你正在编译一些东西,并且在命令行上有-c (它告诉 GCC 在编译源代码后停止,而不是执行链接)。 Since you are also supplying libmxml.a on that same command line, GCC realized that you don't know what you are doing, and warned you to think (more) about it.由于您还在同一命令行上提供libmxml.a ,因此 GCC 意识到您不知道自己在做什么,并警告您(更多)考虑它。

Actually all the previous answers are wrong on the explanation of the first warning I'm afraid (especially the accepted one).实际上,对于第一个警告的解释(尤其是已接受的警告),所有先前的答案都是错误的。 The warning that the static library is not portable most likely stems from the fact that you had the path to it hard-coded (it has to do with the fact that the resulting libgstmatroskademux.la file will actually contain this path).静态库不可移植的警告很可能源于您拥有硬编码的路径(这与生成的libgstmatroskademux.la文件实际上包含此路径的事实有关)。 So instead of relying on pkg-config or gcc itself to do the search of static libraries for you (which would ensure that your project compiles successfully on multiple platforms and distros) you've used fixed paths for the static library (and said path would most likely be different on other platforms).因此,与其依赖pkg-configgcc本身为您搜索静态库(这将确保您的项目在多个平台和发行版上成功编译),您已经使用了静态库的固定路径(并且所述路径将在其他平台上很可能会有所不同)。 The fix is indeed to pass -L$(PATH) -lmxml to gcc (as suggested by the second part of the accepted answer) as this will ensure greater compatibility and will make the Linking the shared library ... against the static library ... is not portable!修复确实是将-L$(PATH) -lmxmlgcc (如已接受答案的第二部分所建议),因为这将确保更大的兼容性,并使Linking the shared library ... against the static library ... is not portable! error go away as well.错误也消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM