gcc中链接器标志的正确顺序是什么? [英] What is the right order of linker flags in gcc?

查看:192
本文介绍了gcc中链接器标志的正确顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我会编译一个需要特定库的程序,例如数学,通过链接器标志后需要它的源如此:

  gcc foo.c -lm 

然而,似乎旧版本的gcc与倒序顺序一样工作得很好(让我们称之为 BAD ORDER ):

  gcc -lm foo.c 

如果我正在尝试编译的一些流行的开源项目没有使用后者,我不担心它,而我的版本 gcc (或者它是 ld 这就是问题所在)只能在前一种情况下工作(在我看来也是正确的)



我的问题是:什么时候 Bad Order 停止工作,为什么?看起来不支持它打破了传统软件包。

解决方案


为什么?看起来不支持它打破了传统软件包。


何时?



不确定,但我认为GCC 4.5之前。很久以前。随后,默认情况下, - 根据需要选项适用于共享库,
与静态库类似,它们必须晚于对象出现在链接序列中为此他们提供了定义。
这是 gcc / g ++ / gfortran 等工具驱动程序传递给 ld
$ p

为什么? 默认情况下,默认情况下,静态库必须出现
,而默认情况下它们提供的定义为共享库的对象不会执行
- 两者之间的差异通常被隐藏 - 用于连接 libname.a libname.so 约定


这可能是一个无法预料的结果,那些不熟悉的人b $ b以前曾经有过很多运气,他们错误地认为GCC
[编译]链接命令符合正常的Unix模式:

 命令[选项...]文件[文件... ] 

例如

  gcc -lthis -lthat -o prog foo.o bar.o 

目前为止它更糟糕。

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:

gcc foo.c -lm

However, it seems that older versions of gcc work equally well with the reverse order (let's call this BAD ORDER):

gcc -lm foo.c

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 gcc (or is it ld that's the problem?) work only in the former case (also, the correct one in my opinion).

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

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 -l<name> convention for linking either libname.a or libname.so.

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:

command [OPTION...] FILE [FILE...]

e.g.

gcc -lthis -lthat -o prog foo.o bar.o

now fare much worse with it.

这篇关于gcc中链接器标志的正确顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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