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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8352617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:40:46  来源:igfitidea点击:

What is the meaning of "Warning: Linking the shared library against static library is not portable"?

clinuxwarningsshared-librariesstatic-libraries

提问by Jeegar Patel

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. Now to use some function of libmxml.a in that plugin I have added $(PATH)/libmxml.ain the GST_CFLAGSvariable in the make file. Now, when I do makeand make install, the plugin works fine, but I still get this warning.

已经有一个用于编译 gstreamer 插件的自动生成的 make 文件。现在要在该插件中使用 libmxml.a 的某些功能,我已将其添加到 make 文件$(PATH)/libmxml.aGST_CFLAGS变量中。现在,当我执行makeand 时make install,插件工作正常,但我仍然收到此警告。

采纳答案by Jan Hudec

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. 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.

第一个警告来自 libtool。它告诉你,你要求的操作会在不同的系统上做不同的事情,其中​​一些事情可能不是你想要的。通常它只会以各种惊人的方式失败,因为进入共享和静态库的代码需要使用不同的编译器标志进行编译。

The second warning is from gcc. It is telling you that providing static library when compilingis pointless. That's because you have $(PATH)/libmxml.ain CFLAGS, where it has no business of being. In fact, most of the time you should nothave $(PATH)/libmxml.a, but -L$(PATH) -lmxmlinstead. That should still go in LDFLAGS, but gcc won't complain if this makes it to the compiler command-line too.

第二个警告来自 gcc。它告诉你编译时提供静态库是没有意义的。那是因为你已经$(PATH)/libmxml.a进入了CFLAGS,它与存在无关。事实上,大多数的时候,你应该不会$(PATH)/libmxml.a,但-L$(PATH) -lmxml不是。那应该仍然进入LDFLAGS,但是如果这也进入编译器命令行,gcc 不会抱怨。

回答by Jan

Ensure that object files in libmxml.awere built with -fPIC. It's necessary to build a shared library. See also http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

确保中的目标文件libmxml.a是用-fPIC. 有必要建立一个共享库。另见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

回答by Employed Russian

Linking the shared library libgstmatroskademux.la against the static library

将共享库 libgstmatroskademux.la 链接到静态库

This is warning you that if you e.g. tried to build this on 64-bit Linux, it would likely fail. That's because on x86_64, all code that gets linked into a shared library mustbe compiled with -fPICflag, and code that lives in .alibraries usually isn't.

这是警告您,例如,如果您尝试在 64 位 Linux 上构建它,它可能会失败。这是因为在 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 -con the command line (which tells GCC to stop after compiling source, and notperform linking). Since you are also supplying libmxml.aon that same command line, GCC realized that you don't know what you are doing, and warned you to think (more) about it.

这是警告你你有一个虚假的命令行。很可能你正在编译一些东西,并且-c在命令行上有(它告诉 GCC 在编译源代码后停止,而不是执行链接)。由于您也在libmxml.a同一命令行上提供,GCC 意识到您不知道自己在做什么,并警告您要(更多)考虑它。