5

使用 GNU ld 2.21 运行 Debian/Linux x86_64。

很简单,如果我链接到

ld -o main main.o /usr/lib/crti.o /usr/lib/crt1.o /usr/lib/crtn.o -lc -lm

它有效,但是当我链接时

ld -r -o main1.o main.o /usr/lib/crti.o /usr/lib/crt1.o /usr/lib/crtn.o -lc -lm

它抱怨

ld: cannot find -lc
ld: cannot find -lm

我实际上并没有尝试以这种方式编译代码,而是试图弄清楚为什么其他人的测试以查看库是否存在不起作用。(因此我不太明白发生了什么ld……通常我只是使用 GCC 来链接)

为什么告诉ld以可重定位方式链接会使其突然无法找到库?如果我只是想测试它的-lm存在,我应该怎么做

ld -r -lm

这样它会找到图书馆?

如果你想看我正在处理的源码,你可以在这里下载:https ://github.com/jeremysalwen/ESPS (注意,第一次提交是原始源代码,后面的都是更改我亲手制作的。)

4

2 回答 2

3

macOS X

在 MacOS X 上,手册页对选项ld非常明确-r

-r合并目标文件以生成另一个文件类型为 MH_OBJECT 的 mach-o 目标文件。

所以,如果你在 MacOS X 上,问题是它-lm不是 Mach-O 目标文件,也不是-lc. 但是,理论上,如果您有目标文件main.oobj1.o并且obj2.o您这样做:

cp obj1.o ./-lm
cp obj2.o ./-lc
ld -r -o main1.o main.o -lm -lc

那么它可能会起作用。在实践中,它没有,并且在您得到的错误中:

ld: warning: unexpected dylib (/usr/lib/libm.dylib) on link line
ld: warning: unexpected dylib (/usr/lib/libc.dylib) on link line

但是,运行:

ld -r -o main1.o -arch x86_64 main.o obj1.o obj2.o

装载机没有任何抱怨的情况下工作。

Linux

On Linux the man page for ld is less explicit, but says:

-i Perform an incremental link (same as option -r).

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

This option does the same thing as -i.

Reading between the lines, this also takes object files and converts them to object files; it does not add libraries into the mix. If you think about it, object files are not created containing references to libraries.

So, although there might be platforms where it is possible to specify libraries to the linker (loader) when using the -r option, there are others where it is not.

Workarounds

The original problem is to establish whether the libraries are present. Why not mimic what autoconf does, and create a main.c that would, for preference, contain a reference to a symbol defined in the library, but which could simply contain:

int main(void){return 0;}

and compile and link it with the C compiler:

cc -o main main.c -lm -lc

If it doesn't work, then one of the libraries is missing. If you've already checked that -lc is present, then you can infer that -lm is missing.

于 2011-07-07T00:53:08.220 回答
0

What is echo $LD_PRELOAD showing you?

Perhaps the error message is saying ld is unable to find the .so of the linked libraries. You could help by setting LD_PRELOAD to point to those .so files.

于 2011-07-07T00:56:31.293 回答