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
258 views
in Technique[技术] by (71.8m points)

c - What is the difference between linking and using a header file?

For example, what is the difference between linking two files

gcc -c func.c 
gcc -c main.c 
gcc func.o main.o -o main 

and using a header file

#include func.h

int main{
....
.. 
}

if they seem to accomplish the same thing?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

They don't "accomplish the same thing". They accomplish complementary parts of the same thing. Typically you have to do both: doing just one, or just the other, is totally insufficient.

Here's an imperfect analogy: Suppose you want an addition on your house. At the start of the project, you sign your name on a contract with the builder, specifying the size and finish of the addition. At the end of the project, you sign your name on a check to pay the builder for his work. What's the difference between signing the contract and signing the check? Why do you have to do both, if they both do the same thing, namely getting you your new addition?

Including a header is (sort of) like signing a contract. When you say #include <math.h>, you're saying "Hey, compiler, there are some math functions. Like sqrt(), which accepts a double and returns a double. So if I write int x = sqrt(144), make sure you do a conversion from int to double before passing the number to sqrt, and make sure you do a conversion from double back to int before assigning the result to x."

Linking against a library is (sort of) like paying your bill. (Actually, if I'm honest, it's almost nothing like paying a bill, but bear with me.) When you put -lm at the end of the command line, you're saying "Hey, compiler, those math functions we agreed on, the ones whose details are specified in the contract called <math.h>, the ones I've been calling, now I'm paying up, here's where those functions actually are."


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

...