C中的分层链接 [英] Hierarchical Linking in C

查看:14
本文介绍了C中的分层链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以分层方式链接三个文件.

//a.cint fun1(){...}int fun2(){...}//b.c外部 int 参数;int fun3(){...//这里使用参数}//main.cint 参数 = 1;int main(){...//使用 fun1 fun2 fun3}

所以,我首先将三个文件分别编译成目标文件a.ob.omain.o.然后我想将 a.ob.o 组合成另一个目标文件 tools.o.并最终使用tools.omain.o 生成可执行文件.

但是,当我尝试像 ld -o tools.o ao bo 一样组合 aobo 时,链接器会显示 未定义对参数"的引用.如何将这些目标文件链接到中间目标文件?

解决方案

您希望 -r 选项生成一个可重定位的目标文件(认为可重用"):

ld -o tools.o -r a.o b.o

<小时>

工作代码

abmain.h

extern void fun1(void);外部无效乐趣2(无效);外部无效乐趣3(无效);外部 int 参数;

交流

#include <stdio.h>#include "abmain.h"void fun1(void){printf("%s
", __func__);}void fun2(void){printf("%s
", __func__);}

b.c

#include <stdio.h>#include "abmain.h"void fun3(void){printf("%s (%d)
", __func__, ++parameter);}

main.c

#include <stdio.h>#include "abmain.h"int 参数 = 1;int main(void){fun1();fun3();fun2();fun3();return 0;}

编译与执行

$ gcc -Wall -Wextra -c a.c$ gcc -Wall -Wextra -c b.c$ gcc -Wall -Wextra -c main.c$ ld -r -o tools.o a.o b.o$ gcc -o abmain main.o 工具.o$ ./abmain乐趣1乐趣3 (2)乐趣2乐趣3 (3)$

在 Mac OS X 10.11.6 和 GCC 6.1.0(以及 XCode 7.3.0 加载程序等)上得到证明.然而,-r 选项至少从 第 7 版 Unix(大约 1978 年),因此它可能适用于大多数基于 Unix 的编译系统,即使它是更广泛使用的选项之一.p>

I want to link three files but in hierarchical way.

// a.c
int fun1(){...}
int fun2(){...}

// b.c
extern int parameter;
int fun3(){...//using parameter here}

// main.c
int parameter = 1;
int main(){...// use fun1 fun2 fun3}

So, I first compile three files separately into object file a.o, b.o and main.o. And then I want to combine a.o and b.o into another object file tools.o. And eventually use tools.o and main.o to generate executable file.

But, when I try to combine a.o and b.o like ld -o tools.o a.o b.o, the linker says undefined reference to 'parameter'. How could I link those object files into an intermediate object file?

解决方案

You want the -r option to produce a relocatable object file (think 'reusable'):

ld -o tools.o -r a.o b.o


Working code

abmain.h

extern void fun1(void);
extern void fun2(void);
extern void fun3(void);
extern int parameter;

a.c

#include <stdio.h>
#include "abmain.h"
void fun1(void){printf("%s
", __func__);}
void fun2(void){printf("%s
", __func__);}

b.c

#include <stdio.h>
#include "abmain.h"
void fun3(void){printf("%s (%d)
", __func__, ++parameter);}

main.c

#include <stdio.h>
#include "abmain.h"

int parameter = 1;
int main(void){fun1();fun3();fun2();fun3();return 0;}

Compilation and execution

$ gcc -Wall -Wextra -c a.c
$ gcc -Wall -Wextra -c b.c
$ gcc -Wall -Wextra -c main.c
$ ld -r -o tools.o a.o b.o
$ gcc -o abmain main.o tools.o
$ ./abmain
fun1
fun3 (2)
fun2
fun3 (3)
$

Proved on Mac OS X 10.11.6 with GCC 6.1.0 (and the XCode 7.3.0 loader, etc). However, the -r option has been in the ld command on mainstream Unix since at least the 7th Edition Unix (circa 1978), so it is likely to be available with most Unix-based compilation systems, even if it is one of the more widely unused options.

这篇关于C中的分层链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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