| Start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| File Types Compiling C programs requires you to work with five kinds of files: **Source files**: These files contain function definitions, and have names which end in `.c` by convention. Note: `.cc` and `.cpp` are C++ files; **not** C files. **Header files**: These files contain function prototypes and various pre-processor statements (see below). They are used to allow source code files to access externally-defined functions. Header files end in `.h` by convention. **Object files**: These files are produced as the output of the compiler. They consist of function definitions in binary form, but they are not executable by themselves. Object files end in `.o` by convention, although on some operating systems (e.g. Windows, MS-DOS), they often end in `.obj`. **Binary executables**: These are produced as the output of a program called a "linker". The linker links together a number of object files to produce a binary file which can be directly executed. Binary executables have no special suffix on Unix operating systems, although they generally end in `.exe` on Windows.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Linker The job of the linker is to link together a bunch of object files ( During the link process, the linker will pick up all the object modules specified on the command line, add some system-specific startup code in front and try to resolve all external references in the object module with external definitions in other object files (object files can be specified directly on the command line or may implicitly be added through libraries). It will then assign load addresses for the object files, that is, it specifies where the code and data will end up in the address space of the finished program. Once it's got the load addresses, it can replace all the symbolic addresses in the object code with "real", numerical addresses in the target's address space. The program is ready to be executed now. This includes both the object files that the compiler created from your source code files as well as object files that have been pre-compiled for you and collected into library files. These files have names which end in Implicit invocation of the linkerLike the pre-processor, the linker is a separate program, often called This line tells the compiler to link together three object files ( Explicit invocation of the linkerIt is possible to invoke the linker directly, but this is seldom advisable, and is typically very platform-specific. That is, options that work on Linux won't necessarily work on Solaris, AIX, macOS, Windows, and similarly for any other platform. If you work with GCC, you can use Options for the linkerThe linker also takes some arguments to modify it's behavior. The following command would tell gcc to link This is actually (more or less) equivalent to (although Note that on many systems, if you are using mathematical functions (from Other compilation optionsThis is all you need to know to begin compiling your own C programs. Generally, we also recommend that you use the The If you want the compiler to throw more warnings at you (including variables that are declared but not used, forgetting to return a value etc.), you can use this set of options, as Note that |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Compiler After the C pre-processor has included all the header files and expanded all macros, the compiler can compile the program. It does this by turning the C source code into an object code file, which is a file ending in In general, the exact sequence how to invoke a C compiler depends much on the system that you are using. Here we are using the GCC compiler, though it should be noted that many more compilers exist:
If your entire program is in one source code file, you can instead do this: This tells the compiler to run the pre-processor on In general the compiler takes four steps when converting a
Note also that the name of the compiler we are using is GCC, which stands for both "GNU C compiler" and "GNU compiler collection", depending on context. Other C compilers exist. For Unix-like operating systems, many of them have the name The POSIX standards currently mandates Note: The |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Preprocessor Before the C compiler starts compiling a source code file, the file is processed in a preprocessing phase. This phase can be done by a separate program or be completely integrated in one executable. In any case, it is invoked automatically by the compiler before compilation proper begins. The preprocessing phase converts your source code into another source code or translation unit by applying textual replacements. You can think of it as a "modified" or "expanded" source code. That expanded source may exist as a real file in the file system, or it may only be stored in memory for a short time before being processed further. Preprocessor commands start with the pound sign ("#"). There are several preprocessor commands; two of the most important are: becomes
becomes: At first approximation, this effect is roughly the same as with inline functions, but the preprocessor doesn't provide type checking for #include is used to access function definitions defined outside of a source code file. For instance: causes the preprocessor to paste the contents of will be changed to: if A or B were defined somewhere in the project before. If this is not the case, of course the preprocessor will do this: This is often used for code, that runs on different systems or compiles on different compilers. Since there are global defines, that are compiler/system specific you can test on those defines and always let the compiler just use the code he will compile for sure. // up to the end of the line, or a combination of opening /* and closing */ comment brackets.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The Translation Phases As of the C 2011 Standard, listed in §5.1.1.2 Translation Phases, the translation of source code to program image (e.g., the executable) are listed to occur in 8 ordered steps.
An implementation of a C compiler may combine several steps together, but the resulting image must still behave as if the above steps had occurred separately in the order listed above. Remarks
Compilers on POSIX platforms (Linux, mainframes, Mac) usually accept these options, even if they are not called
|
Read Article
https://develop.horje.com/learn/1434/reference