Pages

Showing posts with label compilation. Show all posts
Showing posts with label compilation. Show all posts

Sunday, May 8, 2016

Compile C++ program using C++11

C++11 is the standard of the programming language C++ approved by ISO in 12th August, 2011 replacing C++03. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

To compile a C++ program using this version and make use of all the libraries added in this version we need to use the argument c++0x in the command line.

For Eg.:  g++ -std=c++0x hello.cpp -o hello  //Note:its c++ and numeral 0 and x.

Tuesday, August 18, 2015

GCC Compilation Process

GCC compiles a C/C++ program into executable in 4 steps as shown in the diagram below. For example a: "gcc -o hello.exe hello.c" is carried out as follows:

Lets understand this with the  a simple example.

#include <stdio.h>
int main(){
   printf("This is an example of compilation process using gcc");
   return 0;
}

1. Preprocessing: via the GNU C Preprocessor(cpp.exe)  which includes the headers(#include) and expands the macros(#define). The command is cpp hello.exe > hello.i . Note here that the command has cpp not gcc as we are using cpp.exe which is the C preprocessor.

The resultant intermediate file "hello.i" contains the expanded source code. Click on the link below to see how the file looks like.

http://pastebin.com/YBr6yt1s

2. Compilation: The compiler compiles the pre-processed source code into an assembly code for a specified processor.

The command is : gcc -S hello.i

The -S option specifies the pre-processed source code, instead of object code.The resultant assembly is "hello.s".

3. Assembly :  The assembler (as.exe) converts assembly code into machine code.

The command is : as -o hello.o hello.s

4. Linker: Finally the linker links the object code with the library code to produce an executable file.

ld -o hello.exe hello.o ...libraries...

We can see the detailed compilation process by enabling -v(verbose) option.

gcc -v hello.c -o hello.exe