简体   繁体   English

链接共享库的正确方法是什么?

[英]What is the correct way to link against shared libraries?

I'm cross compiling a C application and am linking against the alsa library with -lasound 我正在交叉编译一个C应用程序,并使用-lasound链接alsa库

My newest cross compiler said it can't find the library, so I went exploring in the compiler's library directories and found. 我最新的交叉编译器说它找不到库,所以我去了编译器的库目录并找到了。

libasound.la libasound.so.2 libasound.so.2.0.0 libasound.la libasound.so.2 libasound.so.2.0.0

I did not find a libasound.so , so to work around my problem i created a sym link 我没有找到libasound.so ,所以要解决我的问题,我创建了一个sym链接

ln -s libasound.so.2.0.0 libasound.so

and everything appears to be okay now. 现在一切似乎还可以。 I am positive this is not the right way to do this though. 我很肯定这不是正确的做法。 Am I supposed to use different linker options to link against this? 我应该使用不同的链接器选项来链接吗? And what do each of the different libasound.* files mean? 每个不同的libasound.*文件是什么意思?

The most usual name of libraries in Linux is: Linux中最常用的库名是:

  • The real library is libasound.so.2.0.0 . 真正的库是libasound.so.2.0.0 The last 3 numbers is the library version (major.minor.revision). 最后3个数字是库版本(major.minor.revision)。
  • libasound.so.2 is a symbolic link to the preferred (latest) 2.* version of the library, should you have more than one installed in the system. libasound.so.2是指向库的首选(最新)2. *版本的符号链接,如果您在系统中安装了多个版本。 It is assumed that all the 2.* version are backwards binary compatible. 假设所有2. *版本都是向后二进制兼容的。
  • libasound.la is a text file with a lot of information about the library to be used with libtool . libasound.la是一个文本文件,其中包含有关要与libtool一起使用的库的大量信息。 Useful if you use libtool and the other autotools. 如果你使用libtool和其他autotools很有用。
  • libasound.so is a symbolic link to the library to be used by the toolchain. libasound.so是工具链使用的库的符号链接。 This is the file looked for when you link with -lasound . 这是与-lasound链接时查找的文件。

You are missing the last one, maybe because in debian based systems it is installed only with the libasound-dev package. 你错过了最后一个,也许是因为在基于debian的系统中它只与libasound-dev软件包一起安装。 You can simply create it manually. 您可以手动创建它。 It is not needed during runtime because the library has a SONAME entry in the header: 在运行时期间不需要它,因为库在标头中有一个SONAME条目:

$ objdump -x /usr/lib/libasound.so | grep SONAME
  SONAME               libasound.so.2

That makes the dynamic linker look for that name at runtime, no matter what compiler options you used. 这使得动态链接器在运行时查找该名称,无论您使用何种编译器选项。

I hope I made some sense of this, because it is a bit complicated... 我希望我对此有所了解,因为它有点复杂......

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

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