免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2732 | 回复: 5
打印 上一主题 下一主题

一个GNU ld的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-29 09:31 |只看该作者 |倒序浏览
30可用积分
我的程序分了几个文件夹
每个文件夹都有个makefile 每个下的makefile都用ld把该目录下多个.o连接成一个.o
我用的命令是ld -r -o A.o a.o b.o ... 生成A.o目标文件
然后在顶层的makefile把这些生成的.o 最终连接成可执行文件
我用的命令是ld -s -o exec A.o B.o
但是执行exec的时候出错了

然后我换了种方式 不在每个文件夹下用ld
而是只在顶层的makefile用了一次ld,把所有的.o连接成一个可执行文件
ld -s -o exec a.o b.o c.o ...
执行exec 成功了

想请教的是 为什么会这样的情况?
ld除了重定位数据和符号之外 还做了什么事情?

最佳答案

查看完整内容

ld -s -o exec A.o B.o是不是A.o B.o的目录没写对,多次使用ld一点问题都没有,而且是非常常用的。

论坛徽章:
0
2 [报告]
发表于 2009-12-29 09:31 |只看该作者
ld -s -o exec A.o B.o

是不是A.o B.o的目录没写对,多次使用ld一点问题都没有,而且是非常常用的。

论坛徽章:
0
3 [报告]
发表于 2009-12-29 11:07 |只看该作者
期待高手回答

论坛徽章:
2
摩羯座
日期:2013-10-10 14:29:04天蝎座
日期:2014-01-03 09:14:49
4 [报告]
发表于 2009-12-29 11:38 |只看该作者

回复 #1 eclipse_2 的帖子

是C程序?还是C++? ld手册里提到了partial linking的问题:

-r
--relocatable
    Generate relocatable output—i.e., generate an output file that can in turn serve as input to ld. This is often called partial linking. As a side effect, in environments that support standard Unix magic numbers, this option also sets the output file's magic number to OMAGIC. If this option is not specified, an absolute file is produced. When linking C++ programs, this option will not resolve references to constructors; to do that, use `-Ur'.

    When an input file does not have the same format as the output file, partial linking is only supported if that input file does not contain any relocations. Different output formats can have further restrictions; for example some a.out-based formats do not support partial linking with input files in other formats at all.

论坛徽章:
0
5 [报告]
发表于 2009-12-29 12:49 |只看该作者
其实这种情况我一般都是
ar crs libA.a x.o xx.o ...
最后ld的时候
ld -o output y.o -lA

论坛徽章:
0
6 [报告]
发表于 2009-12-29 12:50 |只看该作者
ld后在ld确实没弄过
就像ar后再ar也是不行的一样^_^
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP