如何确定gcc默认将哪些命令行选项传递给ld? [英] How to determine which command line options gcc passes to ld by default?

查看:764
本文介绍了如何确定gcc默认将哪些命令行选项传递给ld?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑hello world C程序:

hello.c

 #includestdio.h

int main()
{
printf(Hello,World!\\\
) ;
}

如果我打电话:

  $ gcc -c hello.c -o hello.o 

它会产生一个ELF可重定位文件 hello.o

如果我然后打电话:

  $ gcc hello.o -o hello [1] 

它会将hello.o与ld关联起来,并生成一个ELF可执行文件 hello 然而如果我直接调用ld <$ c





$ b $ $ $ $ $ $ ld hello.o -o hello [2]

我收到这些错误:

  /usr/bin/ld.bfd.real:warning:找不到条目符号_start 
test.c :(。text + 0xa):对`puts'的未定义引用

gcc必须将其他选项传递给ld(例如链接C库)。

有没有办法确定命令行gcc正在传递给ld in command [1]

解决方案

是的,您可以使用 gcc -v hello.o -o hello 获取链接。对于你的例子,在我的ubuntu机器上,我得到这个链接(为了可读性编辑为多行):
$ b

  / usr / lib /gcc/x86_64-linux-gnu/4.4.5/collect2 
--build-id
--eh-frame-hdr
-m elf_x86_64
--hash-style = gnu
-dynamic-linker
/lib64/ld-linux-x86-64.so.2
-o hello
-z relro
/ usr / lib /gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5 /../../../../lib/crti.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtbegin.o
-L ​​/ usr /lib/gcc/x86_64-linux-gnu/4.4.5
-L ​​/ usr / lib / gcc / x86_64-linux-gnu / 4.4.5
-L ​​/ usr / lib / gcc / x86_64 -linux-gnu / 4.4.5 /../../../../ lib
-L ​​/ lib /../ lib
-L ​​/ usr / lib /../ lib
-L ​​/ usr / lib / gcc / x86_64-linux-gnu / 4.4.5 /../../ -L / usr / lib / x86_64-linux-gnu
hello.o
-lgcc
- 需要的-lgcc_s - 不需要的
-lc
-lgcc
- 需要的-lgcc_s - 无-as-needed
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtend.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/。 ./。 ./../../lib/crtn.o

请注意 collect2 只是 ld 的别名。


Consider the hello world C program:

hello.c:

#include "stdio.h"

int main()
{
        printf("Hello, World!\n");
}

If I call:

$ gcc -c hello.c -o hello.o

It will produce an ELF Relocatable File hello.o

If I then call:

$ gcc hello.o -o hello            [1]

It will link hello.o with ld and produce an ELF Executable File hello

However if I call ld directly [2] instead of [1]:

$ ld hello.o -o hello             [2]

I get these errors:

/usr/bin/ld.bfd.real: warning: cannot find entry symbol _start
test.c:(.text+0xa): undefined reference to `puts'

gcc must be passing other options to ld (to link the C library for example).

Is there anyway to determine exactly what the command-line gcc is passing through to ld in command [1] ?

解决方案

Yes, you can use gcc -v hello.o -o hello to get the link line. For your example on my ubuntu machine, I get this link line (edited to be multiline for readability):

/usr/lib/gcc/x86_64-linux-gnu/4.4.5/collect2
--build-id
--eh-frame-hdr
-m elf_x86_64
--hash-style=gnu
-dynamic-linker
/lib64/ld-linux-x86-64.so.2
-o hello
-z relro
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crti.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtbegin.o
-L/usr/lib/gcc/x86_64-linux-gnu/4.4.5
-L/usr/lib/gcc/x86_64-linux-gnu/4.4.5
-L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib
-L/lib/../lib
-L/usr/lib/../lib
-L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../.. -L/usr/lib/x86_64-linux-gnu
hello.o
-lgcc
--as-needed -lgcc_s --no-as-needed 
-lc
-lgcc
--as-needed -lgcc_s --no-as-needed
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtend.o
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crtn.o

Note that collect2 is just an alias for ld.

这篇关于如何确定gcc默认将哪些命令行选项传递给ld?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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