Wednesday 16 November 2011

Are all the functions in a library added to an .EXE file when the library is linked to the objects? in C programming

Are all the functions in a library added to an .EXE file when the library is linked to the objects?

No. When the linker is invoked, it will look for “unresolved externals.” This means that it will poll your
library files for functions that were not defined in your source code files. After it finds an unresolved external function, it pulls in the object code (.obj) which contains that function’s definition. Unfortunately, if this function was compiled with a source file that contained other function definitions, those functions are included also. You therefore might have unwanted and unneeded code unnecessarily pulled into your executable information. This is why it is important to keep your library functions contained within their own source file—otherwise, you might be wasting precious program space. Some compilers contain special “smart” linkers that can detect unneeded functions such as these and discard them so that they don’t make their way into your program.

Here is an example: Suppose that you have two source files, libfunc1.c and libfunc2.c. Each contains
functions you want to put in a library.
The source file libfunc1.c contains the following two functions:

void func_one()
{
...
}
void func_two()
{
...
}
The source file libfunc2.c contains the following function:
void func_three()
{
...
}

Now suppose that you have compiled these two source code files into a library named myfuncs.lib. Suppose that a program linked with myfuncs.lib contains a call to func_one(). The linker will search the myfuncs library to pull in the object code that contains the definition of the func_one() function. Unfortunately, the unc_one() function was compiled with the same source file that contains the definition for the func_two() function, and the linker will be forced to pull in the func_two() function even though your program doesn’t use it. Of course, this assumes that func_one() does not contain a call to func_two(). If a program were to contain a call to func_three(), only the object code for func_three() would be pulled in because it was compiled in its own source file.

 Generally, you should keep library functions contained within their own source files. This organization helps your programs to be more efficient because they will be linked only with the functions they really need, and not other functions they don’t need. This also helps in a team development situation in which source code files are continually checked in and checked out. If a programmer is going to perform maintenance on a function that is contained within its own source file, they can focus on that one function. If the source file were to contain several other function definitions that needed maintenance, other programmers would not be able to check out the other functions because they are contained in one source file.

Cross Reference:

XVIII.8: Can multiple library functions be included in the same source file?
XVIII.9: Why should I create a library?

No comments:

Post a Comment