LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2062|回复: 9

gcc可以在运行时指定静态连接器吗?

[复制链接]
发表于 2006-2-18 13:52:13 | 显示全部楼层 |阅读模式
动态连接器可以修改specs完成。
那静态连接器的选择是否也可以在specs中修改?或者通过运行时参数设定呢?
发表于 2006-2-18 14:15:16 | 显示全部楼层
我认为:(因为还完全测试)

静态,和动态是一样的,也就是说,动态连接的是哪个,静态连接的也是哪个。只是把所有需要的部分都包括进去程序里边而已。

gcc编译的时候,前边加上了 -static。
或者 ./configure 的时候,加上 --enable-static。

当然,有的包不一定遵循这个规矩,那可以尝试在 CFLAGS 里边加入 "-static"。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-18 15:12:36 | 显示全部楼层
版主误会我的意思了。
我不是说入如何静态编译gcc,而是gcc编译别的程序的时候如何选择ld。
比如/usr/bin/ld,和/tools/bin/ld
我现在运行gcc -o hello hello.c 。系统默认的是/usr/bin/ld连接的。
可以在gcc -o hello hello.c 时添加某个参数,指定后面用到的ld嘛?
回复 支持 反对

使用道具 举报

发表于 2006-2-18 15:15:03 | 显示全部楼层
可以试试设置环境变量
LD="/tools/bin/ld"
然后再编译
不过这个不叫静态编译。
回复 支持 反对

使用道具 举报

发表于 2006-2-18 15:52:28 | 显示全部楼层
1。可以通过指定PATH来解决。
把含有 ld 的目录放在 PATH 最前边就可以了。

加了的效果:
checking for ld used by GCC... /tools/i686-pc-linux-gnu/bin/ld
checking if the linker (/tools/i686-pc-linux-gnu/bin/ld) is GNU ld.

是没加的效果:
checking for ld used by GCC... /usr/i686-pc-linux-gnu/bin/ld
checking if the linker (/usr/i686-pc-linux-gnu/bin/ld) is GNU ld... yes
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-18 15:53:51 | 显示全部楼层
通过环境变量不行吧。
根据LFS6.1第一编译binutil,第一次编译gcc.
$LFS/tools/bin/gcc 使用的就是$LFS/tools/bin/ld
而主机系统的gcc用的则是/usr/bin/ld。

如果是通过环境变量设置,不管运行哪里的gcc,输出的应该都是LD的值吧?


编译GCC时,指定了binutils的搜索路径,这个路径难道是锁定到GCC程序内部了吗?gcc生成以后就不可以更改了? 就好比gcc编译其他程序时,把动态连接器的路径锁定到程序内部?
回复 支持 反对

使用道具 举报

发表于 2006-2-18 16:14:59 | 显示全部楼层
简单问一下,你怎么看出 gcc 是用哪个 ld 的?

我找了一下,没找到方法。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-18 16:44:54 | 显示全部楼层
按照版主说的,我试过了。
gcc -c hello.c 或者gcc -print-prog-name=ld可以查看gcc使用的ld,LFS里面有啊

反复尝试,结果如下:
如果gcc能够找到自己编译时用的那个ld,则是首选。在LFS中,/tools/bin/gcc 始终使用/tools/i386-..../bin/ld,无论/usr/bin里面是否存在ld.
如果我故意把编译gcc时用的ld和as更名,也就是在/tools/i386.../bin下找不到ld了,这时的gcc才会去从PATH里面寻找。

看来gcc还是留恋娘家人啊,野花没有家花香的道理。哈哈
回复 支持 反对

使用道具 举报

发表于 2006-2-18 17:00:26 | 显示全部楼层
看来是我的RPWT了。。。
主系统运行这个命令没效果,chroot到新的系统,就看到效果了。(原因见后)

你说的,和我这里的测试结果一致。

# gcc -print-prog-name=ld
/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../i686-pc-linux-gnu/bin/ld
# export PATH=/tools/binPATH
# /usr/bin/gcc -print-prog-name=ld
/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../i686-pc-linux-gnu/bin/ld



我现在明白为什么我的主系统没结果了。
跟踪了一下,结果如下:
[html]
.
.
.
access("/usr/lib/gcc/i686-pc-linux-gnu/specs", R_OK) = -1 ENOENT (No such file or directory)
access("/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/", X_OK) = 0
stat64("/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/ld", 0xbf9d7e7c) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/ld", 0xbf9d7e7c) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/gcc/i686-pc-linux-gnu/ld", 0xbf9d7e7c) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/ld", 0xbf9d7e7c) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/gcc/i686-pc-linux-gnu/ld", 0xbf9d7e7c) = -1 ENOENT (No such file or directory)
stat64("/usr/libexec/gcc/i686-pc-linux-gnu/4.0.2/ld", 0xbf9d7e7c) = -1 ENOENT (No such file or directory)
stat64("/usr/libexec/gcc/i686-pc-linux-gnu/ld", 0xbf9d7e7c) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/ld", 0xbf9d7e7c) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/gcc/i686-pc-linux-gnu/ld", 0xbf9d7e7c) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../i686-pc-linux-gnu/bin/i686-pc-linux-gnu/4.0.2/ld", 0xbf9d7e7c) = -1 ENOENT (No such file or directory)
stat64("/usr/lib/gcc/i686-pc-linux-gnu/4.0.2/../../../../i686-pc-linux-gnu/bin/ld", 0xbf9d7e7c) = -1 ENOENT (No such file or directory)
.
.
.
[/html]
证明的确是先找本身体系中的,找不到,再在 PATH 中取,不过这个已经不是gcc的工作了。。活活。。。

谢谢楼上的指正。:)。
回复 支持 反对

使用道具 举报

发表于 2006-9-15 14:28:51 | 显示全部楼层
挖个坟。。。
在gccint.pdf中有关于collect2的描述
The program collect2 is installed as ld in the directory where the passes of the compiler
are installed. When collect2 needs to find the real ld, it tries the following file names:
• ‘real-ld’ in the directories listed in the compiler’s search directories.
• ‘real-ld’ in the directories listed in the environment variable PATH.
• The file specified in the REAL_LD_FILE_NAME configuration macro, if specified.
• ‘ld’ in the compiler’s search directories, except that collect2 will not execute itselfrecursively.
• ‘ld’ in PATH
“The compiler’s search directories” means all the directories where gcc searches for passes
of the compiler. This includes directories that you specify with ‘-B’.
Cross-compilers search a little differently:
• ‘real-ld’ in the compiler’s search directories.
• ‘target-real-ld’ in PATH.
• The file specified in the REAL_LD_FILE_NAME configuration macro, if specified.
• ‘ld’ in the compiler’s search directories.
• ‘target-ld’ in PATH.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表