- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
使用 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 (注意,第一次commit是原来的源码,后面的commit是我个人修改的。)
最佳答案
在 MacOS X 上,ld
的手册页对 -r
选项非常明确:
-r
Merges object files to produce another mach-o object file with file type MH_OBJECT.
因此,如果您使用的是 MacOS X,问题在于 -lm
不是 Mach-O 目标文件,-lc
也不是。但是,理论上,如果您有目标文件 main.o
、obj1.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 上,ld
的手册页不太明确,但是说:
-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
.
字里行间,这也获取目标文件并将它们转换为目标文件;它不会将库添加到组合中。如果您考虑一下,目标文件不会创建包含对库的引用。
因此,虽然在使用 -r
选项时可能有一些平台可以为链接器(加载器)指定库,但在其他平台上则不能。
最初的问题是确定库是否存在。为什么不模仿 autoconf
的作用,并创建一个 main.c
,它会优先包含对库中定义的符号的引用,但它可以简单地包含:
int main(void){return 0;}
并用 C 编译器编译和链接它:
cc -o main main.c -lm -lc
如果它不起作用,那么其中一个库丢失了。如果您已经检查过 -lc
存在,那么您可以推断 -lm
缺失。
关于linux - 为什么 -r 选项(可重定位)使 ld 找不到任何库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6570034/
我有以下代码行 #define SCB_VTOR SCB_VTOR_REG(SystemControl_BASE_PTR) #define SCB_VTOR_REG(base)
我已经用 C 语言编写了这个函数(函数应该接收一个 char*,分配必要的空间,并插入字符,给出一个指向指针后面字符索引的指针) void add_to_str(char *character, ch
我正在构建一个 ELF 二进制文件,它需要能够在运行时处理和反转其自身的重定位。 (显然,反转将发生在单独的缓冲区中,而不是原始代码页中。)这样做的目的是内存中的模块内容可以进行 HMAC 运算,并与
我花了很多时间来查找错误原因。下面是我的一段 C++ 程序,它使用 vector 来存储数据。我已经看到,当 vector 增加自身大小时,所有引用都会发生变化。因此,通过 back() 对象保存对
首先,对于我的代码中可能出现的废话,我深表歉意,我还只是个初学者。我正在尝试用 编译这段代码 #include #include #include //initial conditions //#de
基本上,我一直在图像上测试 ImageMapster 插件。但是,我希望每个区域标签的工具提示(或信息面板)彼此相邻。 > http://jsfiddle.net/jagmitg/eQCL6/ 目前,
我正在使用 Devtoolset-7在 CentOS 7 上构建了 Boost 1.65.1 w/它。但是当我链接我的应用程序时,我得到了以下信息: /opt/rh/devtoolset-7/root
我正在 Linux 下编写一个简单的用户空间 ELF 加载程序(为什么?为了“好玩”)。目前我的加载器非常简单,旨在仅加载包含位置无关代码的静态链接 ELF 文件。 通常,当一个程序被内核的 ELF
GWT 将生成一个 gwt-unitCache包含一些缓存文件的目录。经过几天的工作,该目录可能会产生超过 1GB 的缓存文件。我担心生成这些文件可能会损坏我的 SSD 硬盘。 我创建了一个 2GB
假设我正在使用一个 int a vector : vector a {1, 2, 34, 1222, 0}; 然后,我想像这样 push_back 一些数据: a.push_back (data);
我刚刚开始进行嵌入式 arm 开发,有一段代码真的让我很烦恼: /* Initialize the relocate segment */ pSrc = &_etext; pDest = &_srel
所以我有一个 block 在 iPhone 内置加速度计的帮助下移动,另一个 block 随机出现在屏幕上。我正在尝试使用 if 语句来确定移动 block 是否点击或触摸了固定目标 block ,如
我想弄清楚重定位是如何工作的,但我似乎无法理解它。 This document描述了在重定位 ELF 文件时可能遇到的不同类型。 我们以 R_ARM_ALU_SB_G0_NC (#70) 为例。 类型
我正在尝试通过 OS X 中的 __builtin_return_address() 获取返回地址: /* foo.c */ #include void foo() { printf("re
我正在寻找与 Gulp 一起使用的插件链,它提供: 源映射支持 少 缩小 串联 URL 替换( rebase )以解决重定位/连接 我目前有前四个,但我找不到现有插件的组合也能给我最后一个(URL r
我正在编写一些适用于 .o 文件的自定义 ELF 二进制后处理代码。部分处理包括对二进制数据执行重定位。输入文件由 GNU 汇编程序根据我自己的汇编代码生成。 看看 GNU 汇编程序生成的这些疯狂的重
所以我一直在编写 32 位代码,昨天我需要构建一个 dll,但我遇到了一些问题。反正我解决了here . 不幸的是,即使我认为一切正常,但当我将程序和 makefile 移动到其他运行 64 位的计算
在今天下午的华为全场景智慧生活新品发布会上,华为还发布了一款华为儿童手表 4X,在防水和定位性能方面进行了升级。 据介绍,华为儿童手表 4X 采用机甲设计风格,一体化表带,防水升级到 50 米
我正在尝试编译的项目一点也不复杂,除了标准库和一个独立的库(一切都在另一个系统上编译正常)之外,什么都不引用。正如标题所示,它甚至不能链接到标准库中的某些东西,因为那里的东西应该没有用 -fPIC 编
我是一名优秀的程序员,十分优秀!