gcc链接库搜索顺序;路径加上静态与共享 [英] gcc linker library search order; paths plus static vs shared

查看:135
本文介绍了gcc链接库搜索顺序;路径加上静态与共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通读gcc手册,我相信以下两个说法是正确的:

Reading through the gcc manual, I believe the following two statements are true:

  1. 在命令行中指定的库搜索路径先于默认"路径(我认为这意味着LIBRARY_PATH环境变量中的内容)被搜索

  1. Library search paths specified on the command line are searched before "default" paths (which I assume means stuff in the LIBRARY_PATH environment variable)

共享库将优先链接到静态库(在没有标记表明不这样做的情况下)

Shared libraries will be linked in preference to static libraries (in the absence of flags saying to do otherwise)

但是,这两个占主导地位?例如,如果我输入

But which of these two dominates? For example, if I type

gcc myprog.cpp -o myprog -Lmypath -lmylibrary

,在mypath中有一个静态库"libmylibrary.a",在LIBRARY_PATH中指定的某个地方有一个共享库"libmylibrary.so",将使用以下哪个库?我的猜测是静态库将被使用(即(1)占主导地位),但是我看到一些有趣的编译错误使我对此猜测表示怀疑,所以我想确保...

and in mypath there is the static library "libmylibrary.a", and in some place specified in LIBRARY_PATH there is a shared library "libmylibrary.so", which of these libraries will get used? My guess would be that the static library will get used (i.e. (1) dominates) but I am seeing some funny compile errors that make me question this guess, so I wanted to make sure...

推荐答案

在您的示例中,静态库libmylibrary.a将被链接到 对任何可能会libmylibrary.alibmylibrary.so的首选项 存在于链接器的默认搜索目录之一中.链接器 在mypath中搜索任何默认位置之前以及之后 它找到一个libmylibrary.alibmylibrary.so 满足-lmylibrary.

In your example, the static library libmylibrary.a will be linked in preference to any libmylibrary.a or libmylibrary.so that might exist in one of the linker's default search directories. The linker searchs in mypath before any of the default places, and as soon as it finds a libmylibrary.a or libmylibrary.so, it looks no further to satisfy -lmylibrary.

如果mypath包含 libmylibrary.alibmylibrary.so,则 后者是首选.

If mypath contained both libmylibrary.a and libmylibrary.so, the latter would be preferred.

我不确定"gcc手册"指的是什么来源,但是 "gcc链接器"是GNU链接器ld,您会发现 命令行选项的含义在手册

I am not sure what source you are referring to by "the gcc manual", but "the gcc linker" is the GNU linker, ld, and you will find the meanings of the commandline options very well explained in its manual

这篇关于gcc链接库搜索顺序;路径加上静态与共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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