Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

compiler errors - Hierarchical Linking in C

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...