![]() |
|
#1
|
|||
|
|||
Compiling/LinkingCan someone please give me a basic rundown of how the compiling and linking stages work. Take for example the basic begining code snippet.
CPP / C++ / C Code:
Ok, the # causes the preprocessor program to run. But what exactly does the preprocessor do??? The include tells the compiler to "include" stdio.h in the the program. Does this mean that the code in stdio.h will be added to my source file or does it just provide a reference to the stdio.h file so it can find the instructions for how to handle printf()???? After compilation and i have the .obj file does the linker add code modules from the program libraries? Does the compiling stage just take the .c/.cpp file and turn everything in the file into machine code and the linking process is what combines the .obj file with the .h file?? Also, In beginning C ivor Horton refers to program libraries as "code modules that are not part of C but provide operations like finding the square root." By program libraries is he refering to Header files and what does he mean by not being part of the C language? Any help is greatly appreciated, Thanks Last edited by admin : 08-Nov-2005 at 10:14.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#2
|
||||
|
||||
Re: Compiling/LinkingHi roostercogburn,
Welcome to the GID Forums. Here is a detailed description of compiling and linking: Compiling: The compiler converts your source code into machine language, and detects and reports errors in the compilation process. The input to this stage is the file you produced during your editing, which is usually referred to as a source file. The compiler can detect a wide range of errors that are due to invalid or unrecognized program code, as well as structural errors where, for example, part of a program can never be executed. The output from the compiler is known as object code and is stored in files called object files, which usually have names with the extension.obj. The compiler can detect several different kinds of errors during the translation process, and most of these will prevent the object file from being created. The result of a successful compilation is a file with the same name that you used for the source file, but with the.obj extension.(that depends on your os) Linking: The linker combines the various modules generated by the compiler from source code files, adds required code modules from program libraries supplied as part of C, and welds everything into an executable whole. The linker can also detect and report errors—for example, if part of your program is missing or a nonexistent library component is referenced. In practice, if your program is of any significant size, it will consist of several separate source code files, which can then be linked. A large program may be difficult to write in one session. By breaking it up into a number of smaller source files, you can make the development of the program a whole lot easier. The source files can be compiled separately, which makes eliminating simple typographical errors a bit easier. Furthermore, the whole program can usually be developed incrementally. Each source file will have its own file name, and the set of source files that make up the program will usually be integrated under a project name, which is used to refer to the whole program. Program libraries support and extend the C language by providing routines to carry out operations that aren’t part of the language. For example, libraries contain routines that support operations such as performing input and output, calculating a square root, comparing two character strings, or obtaining date and time information. A failure during the linking phase means that, once again, you have to go back and edit your source code. Success, on the other hand, will produce an executable file. Now, to the preprocessor: Preprocessing is the first step in the C program compilation stage -- this feature is unique to C compilers. The preprocessor more or less provides its own language which can be a very powerful tool to the programmer. All preprocessor directives or commands begin with a #. Use of the preprocessor is advantageous since it makes:
CPP / C++ / C Code:
This first line of the program is a preprocessing directive, #include. This causes the preprocessor—the first tool to examine source code when it is compiled—to substitute for that line the entire text of the file or other entity to which it refers. In this case, the header file stdio.h—which contains the definitions of standard input and output functions—will replace that line. The angle brackets surrounding stdio.h indicate that the file can be found within one of the locations given to the preprocessor through the search path for header files. Quote:
Quote:
There are two types of libraries: One defined by the programmer. i.e Us. and The standard C library. By mentioning "code modules that are not part of C but provide operations like finding the square root.", he probably means that most of the libraries are user defined and all does not come with the C language. The C standard library includes routines for file input and output, allocating memory, and working with common data such as mathematical functions, character strings, and time values. Since many programs have been written in C, there are a wide variety of other libraries available. All Standard C library entities are declared or defined in one or more standard headers. To make use of a library entity in a program, write an include directive that names the relevant standard header. The full set of 18 Standard C headers constitutes a hosted implementation: <assert.h>, <ctype.h>, <errno.h>, <float.h>, <iso646.h>, <limits.h>, <locale.h>, <math.h>, <setjmp.h>, <signal.h>, <stdarg.h>, <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>, <wchar.h>, and <wctype.h>. -Code extracted from the Web. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
||||||
|
||||||
Re: Compiling/LinkingQuote:
Quote:
There is no need to have #include as the first line. You smply need to be sure it's in the code before anything defined in it is used. Small point, but important. Quote:
#include <xxx.h> tells the preprocessor to search compiler directories themselves for the header file. #include "xxx.h" says search the local directory first, then the compiler directories. This allows the programmer to 'replace' the compiler's header with one of his own. Good luck, have fun, see you after the crash... Quote:
1) One defined by the programmer, i.e Us. 2) The standard C library. 3) The compiler's extended funcionality libraries, i.e. conio.h, dos.h, etc Quote:
Quote:
The book (copyright 1990) is obviously old, so there may have been a few added includes since then. Note math.h is standard. So I wonder why Ivor Horton mentioned "code modules that are not part of C but provide operations like finding the square root" since a standard header is defined? I assume the comment was made before the standards were adopted and math.h was not originally part of the language... __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#4
|
|||
|
|||
Re: Compiling/LinkingQuote:
The code modules he is referring to are library functions. Functions like sqrt() are not part of the language, but they are in the C language standard specifications as standard library functions. What's the difference? Well for one thing the identifier "sqrt" is not a key word nor is it defined in any other way for the language itself. You can make your own function, variable, struct, or whatever you want and call it "sqrt". On the other hand, "if" is a keyword whose defined usage is part of the language and is reserved for the use as defined in the standards document. You can't have a variable named "if". A compiler claiming to be compliant with C standards is required to supply all standard C library functions. In the case of sqrt(), the C standard states that the prototype is in <math.h>. Of course the actual location (in your computer's file system) of the standard headers and the way that library functions are linked into your program's code is implementation-dependent, and have never been any part of standards specifications or other documentation outside of your own compiler and operating system. So, Horton is being very precise and very correct in his terminology. Sometimes these differences are a little too subtle for clear understanding. Sometimes thay are important, other times --- not so much. Regards, Dave |
|
#5
|
||||
|
||||
Re: Compiling/LinkingAhh, thanks Dave. Your depth of knowledge (and quotes) makes this forum well worth the price of admission -- no wait that's not right... Oh well, you know what I mean
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#6
|
|||
|
|||
Re: Compiling/LinkingQuote:
Yep, a bargain even if it cost half as much. Regards, Dave |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Trouble with compiling/linking | mikhail | C Programming Language | 0 | 25-Jun-2004 13:58 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The