C/c++ Tutorial - Compiler Linker Basics

  • Uploaded by: Gurukul24x7
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C/c++ Tutorial - Compiler Linker Basics as PDF for free.

More details

  • Words: 1,026
  • Pages: 3
Introduction to C++. Learning the development tools: The compiler: Compiler is the software that translates the syntactically correct statements of a program into object code or can also produce an executable using a linker and the object code produced. An object code can't be run directly on a machine since it contains information about the object module in addition to the machine instructions. A linker is another program which is invoked upon compiled object files. A linker just identifies the structure of the object files, resolves the functions and object linkages and creates an executable. The Compilation Process: The compiler has a preprocessor, its purpose is to parse and process the statements which are meant written using the preprocessor directives. These directives include statements that begin with a # (pound) sign. So the include statements , define statements ifdef and endif statements are processed by the preprocessor. When the preprocessor is working it is called the first pass. When this phase is successfully over, the second pass starts. The purpose of the second pass is to check the syntax and generate the machine code. While generating the machine code compiler also optimizes the code, optimization leads to smaller and smart code. • • •

C++ enforces static type checking. For example a long variable can't be assigned a double variable. C++ doesn't enforce dynamic type checking, that is type checking while program is running because C++ was designed to produce high speed code just as C. Like C, C++ allows module programming so you can fragment your code into multiple files each describing a useful module or a back end library which can then all be combined together to produce a single executable file.

Function Declaration and Linkage: A function declaration is an “announcement” to the compiler that this is how a function will look like. For example. Void myfunc(int); Looking at this declaration see that the braces are missing and the statement is ended by a semicolon. This tells the compiler that the function's body or the definition is someplace else but this is how it'll look like so for example if a code calls this like shown below, int main() { myfunc(2);

} void myfunc(int a)//this is the definition { cout< is an include directive which includes a file called iostream. There's no restriction on the name of the header file. It may have any extension and any valid name but traditionally the header files have had a .h extension but really its not a requirement. Why the <> brackets? The compiler usually has a configuration file where it stores the default location of where to look for header files and the libraries to link to. The <> brackets tells the compiler that it should look for the file specified within the brackets in the default location and NOT anywhere else. But obviously you would be creating header files of your own and would be including them. Since its a tedious job to put those files in the default location as stored in the configuration file you can also specify the compiler where to look for the header files in addition to the default location. You can do this by specifying the file within quotes “”. So suppose that you are working in your CPP file is present

in the current directory while the header files are present in the directory headers then we can include the file as... #include “headers/myheaderfile.h” The compiler first looks in headers folder which is present in the current directory if it can't find the file then it looks in the <default location>/header that is in the folder header which is present in the default location as specified in the compiler's configuration file. You can thus also include a file present in the default location like, #include “iostream” without any error. However if you have a file named iostream in the current directory where your CPP file is present then instead of the standard header your version of iostream would be included because the order of search is that way when you use quotes. Including C header files Since a lot of code is present in the C standard C library, all of it was included in C++. The header files however were changed to be con formant to C++, these headers were prefixed with letter c with the extension removed. For example, C header

C++ header

String.h

cstring

Stdlib.h

cstdlib

Stdio.h

cstdio

Related Documents


More Documents from ""