GCC连接选项-L:另类的方式如何指定的动态库的路径 [英] gcc Linkage option -L: Alternative ways how to specify the path to the dynamic library

查看:168
本文介绍了GCC连接选项-L:另类的方式如何指定的动态库的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我编译我的源$ C ​​$ c。与-L。动态库libmd5.so可以找到。

If I compile my source code with "-L." the dynamic library libmd5.so can be found.

gcc main.c -g -O2 -Wall -o main -L. -lmd5 -lcr

但是,如果我离开-L - 选择方案,链接器没有找到动态库。我怎样才能改变这种状况,而无需调用-L?

But if I leave the "-L."-option away, the linker does not find the dynamic library. How can I change that without having to invoke "-L."?

(libmd5.so其他信息和libmd5.so.1.0.1位于在/ home /用户/ BA)

(additional info libmd5.so and libmd5.so.1.0.1 are located in /home/user/ba)

推荐答案

有真的没有错,-L标志,所以你不应该着急摆脱它 - 它是在运行时遇到问题,如该系统将不会加载您链接到库?下面是一些选项:

There's really nothing wrong with the -L flag, so you shouldn't try so hard to get rid of it - is it at runtime you have problems, as the system won't load the libraries you link to ? Here's some options :


  • 添加/家庭/用户/ BA加入/etc/ld.so.conf(或你的操作系统类似),之后运行ldconfig。这将是全系统。

  • 将LIBRARY_PATH(链路时间)和LD_LIBRARY_PATH(对于运行时)环境变量。 导出LD_LIBRARY_PATH = / home / user中/ BA 出口LIBRARY_PATH = / home / user中/ BA 。这才会有效果只为当前的shell。

  • 设置中的可执行rpath的(你还需要在 -L。这里虽然)。 -L / home / user中/ BA -Wl,-rpath,/ home / user中/ BA 添加到您的链接标志。这将影响只为你正在做的可执行文件。

  • 将在全系统的库搜索路径共享库,如 / usr / lib目录。这将是全系统。

  • Add /home/user/ba to /etc/ld.so.conf (or similar for your OS) and run ldconfig afterwards. This will be system wide.
  • Set the LIBRARY_PATH(for link time) and LD_LIBRARY_PATH(for run time) environment variable. export LD_LIBRARY_PATH=/home/user/ba and export LIBRARY_PATH=/home/user/ba .This will have effect only for the current shell.
  • Set the rpath in the executable (you still need the -L . here though). Add -L /home/user/ba -Wl,-rpath,/home/user/ba to your linker flags. This will have effect only for the executable you're making.
  • Place your shared libraries in a system wide library search path such as /usr/lib. This will be system wide.

以上有在运行时的效果,以及 - 它会试图找到libmd5.so在/ home /用户/ BA或系统中的其他库搜索路径,当你运行应用程序也是如此。

The above have effect at runtime as well - it'll try to find libmd5.so in /home/user/ba or other library search paths for the system when you run the app as well.

这篇关于GCC连接选项-L:另类的方式如何指定的动态库的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆