gcc中链接器标志的正确顺序是什么? [英] What is the right order of linker flags in gcc?
问题描述
gcc foo.c -lm
然而,似乎旧版本的gcc与倒序顺序一样工作得很好(让我们称之为 BAD ORDER ):
gcc -lm foo.c
如果我正在尝试编译的一些流行的开源项目没有使用后者,我不担心它,而我的版本 gcc
(或者它是 ld
这就是问题所在)只能在前一种情况下工作(在我看来也是正确的)
我的问题是:什么时候 Bad Order 停止工作,为什么?看起来不支持它打破了传统软件包。
为什么?看起来不支持它打破了传统软件包。
何时?
不确定,但我认为GCC 4.5之前。很久以前。随后,默认情况下, 为什么? 默认情况下,默认情况下,静态库必须出现 这可能是一个无法预料的结果,那些不熟悉的人b $ b以前曾经有过很多运气,他们错误地认为GCC 例如 目前为止它更糟糕。 Normally, I would compile a program that requires a specific library, e.g. math, by passing the linker flag after the sources that need it like so: However, it seems that older versions of gcc work equally well with the reverse order (let's call this BAD ORDER): I wouldn't worry about it if some popular open-source projects I'm trying to compile didn't use the latter while my version of My question is: when did the BAD ORDER stop working and why? It seems that not supporting it breaks legacy packages. when did the BAD ORDER stop working and why? It seems that not supporting it breaks legacy packages. When? Not dead sure but I think pre-GCC 4.5. Long ago. Subsequently, the Why? It was considered confusing to inexpert users that static libraries by default has to appear
later that the objects to which they provided definitions while shared libraries by default did
not - the difference between the two typically being concealed by the It was perhaps an unforeseen consequence that inexpert users who
had formerly had a lot of luck with the mistaken belief that a GCC
[compile and] link command conforms to the normal Unix pattern: e.g. now fare much worse with it. 这篇关于gcc中链接器标志的正确顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! - 根据需要
选项适用于共享库,
与静态库类似,它们必须晚于对象出现在链接序列中为此他们提供了定义。
这是 ld $ c的默认选项的更改
$ p
,而默认情况下它们提供的定义为共享库的对象不会执行
- 两者之间的差异通常被隐藏 - 用于连接
libname.a
或 libname.so $ c $
约定
[编译]链接命令符合正常的Unix模式:
命令[选项...]文件[文件... ]
gcc -lthis -lthat -o prog foo.o bar.o
gcc foo.c -lm
gcc -lm foo.c
gcc
(or is it ld
that's the problem?) work only in the former case (also, the correct one in my opinion).
--as-needed
option is operative for shared libraries by default,
so like static libraries, they must occur in the linkage sequence later than the objects for which they provide definitions.
This is a change in the default options that the gcc/g++/gfortran
etc. tool-driver passes to ld
.-l<name>
convention
for linking either libname.a
or libname.so
.command [OPTION...] FILE [FILE...]
gcc -lthis -lthat -o prog foo.o bar.o