Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between linking and binding?

Tags:

c++

c

linker

I was reading about the two things and got confused, what are the differences between the two?

like image 670
user2614516 Avatar asked Oct 04 '14 14:10

user2614516


1 Answers

Binding is a word that is used in more than one context. It always has to do with the connecting of one thing to another however when the act of binding happens can vary.

There is a concept of Binding Time or the point at which some component is bound to some other component. A basic list of binding time is: (1) binding at compile time, (2) binding at link time, (3) binding at load time, and (4) binding at run time.

Binding at compile time happens when the source code is compiled. For C/C++ there are two main stages, the Preprocessor which does source text replacement such as define replacement or macro replacement and the compilation of the source text which converts the source text into machine code along with the necessary instructions for the linker.

Binding at link time is when the external symbols are linked to a specific set of object files and libraries. You may have several different static libraries that have the same set of function names but the actual implementation of the function is different. So you can choose which library implementation to use by selecting different static libraries.

Binding at load time is when the loader loads the executable into memory along with any dynamic or shared libraries. The loader binds function calls to a particular dynamic or shared library and the library chosen can vary.

Binding at run time is when the program is actually running and makes choices depending on the current thread of execution.

So linking is actually just one of the types of binding. Take a look at this stackoverflow static linking vs dynamic linking which provides more information about linking and libraries.

You may also be interested in std::bind in C++ so here is a stackoverflow article std::function and std::bind what are they when they should be used.

The longer you wait before something is bound to something else can provide a needed degree of flexibility in how the software can be used. However often there is a trade off between delaying binding and run time efficiency as well as complexity of the source.

For an example of bind time consider an application that opens a file and reads from the file then closes it. You can pick a couple of different times when the file name is bound to the file open.

You might hard code the file name, binding at compile time, which means that it can only be used with that one file. To change the file name you have to change the source and recompile.

You might have the file name entered by the user such as with a user prompt or a command line argument, binding the file name to the file open at run time. To change the file name you no longer need to recompile, you can just run the program again with a different file name.

like image 127
Richard Chambers Avatar answered Nov 15 '22 03:11

Richard Chambers