Вы находитесь на странице: 1из 5

QUESTION/DISCUSSION

1.How to
a.Compilation process
b.Execute the program
A.Compilation process
Compiling a source code file in C++ is a four-step process. For example, if you have a
C++ source code file named prog1.cpp and you execute the compile command
g++ -Wall -ansi -o prog1 prog1.cpp
The compilation process looks like this:
1.The C++ preprocessor copies the contents of the included header files into the source
code file, generates macro code, and replaces symbolic constants defined
using #define with their values.
2.The expanded source code file produced by the C++ preprocessor is compiled into the
assembly language for the platform.
3.The assembler code generated by the compiler is assembled into the object code for
the platform.
4.The object code file generated by the assembler is linked together with the object
code files for any library functions used to produce an executable file.
By using appropriate compiler options, we can stop this process at any stage.
1.To stop the process after the preprocessor step, you can use the -E option:
g++ -E prog1.cpp
The expanded source code file will be printed on standard output (the screen by
default); you can redirect the output to a file if you wish. Note that the expanded source
code file is often incredibly large - a 20 line source code file can easily produce an
expanded file of 20,000 lines or more, depending on which header files were included.
2.To stop the process after the compile step, you can use the -S option:
g++ -Wall -ansi -S prog1.cpp
By default, the assembler code for a source file named filename.cpp will be placed in a
file named filename.s.
3.To stop the process after the assembly step, you can use the -c option:
g++ -Wall -ansi -c prog1.cpp

By default, the assembler code for a source file named filename.cpp will be placed in a
file named filename.o.
B.Execute the program

Compiling and Executing C program in Ubuntu 11.10


1. Write and save the program

Open a simple text editor (e.g gedit), IDE (Eclipse) or command line code editor
(Nano or Vim). Ill be using gedit as it is very simple to use and its recommended
for beginner programmers. Right Click on Desktop or any directory (Browse File
using Nautilus) and select create new File hello.c (.c extension is used to indicate
that its a c program). Then write a simple program like this (and save the program
press Ctrl+S)
#include<stdio.h>

void main()
{
printf("Hello! This is my first C program with Ubuntu 11.10\n");
/* Do something more if you want */

2. Compile the program


GCC (GNU Compiler Collection) is installed by default, in Ubuntu. To compile the
program, open the terminal and move on to the target directory type the command
(where gcc implies compiler name, then it asks for the file name of the source
program while -o option specifies the file name of the output program)
gcc hello.c -o hello1
If there is no syntax/semantic error in you program then the compiler will
successfully generate an executable file, otherwise fix the problem in your code.
3. Execute the program
To execute the program, you need to run ./hello1

Compiling and Executing C++ program


The steps are almost same as above but you need to install g++ compiler, the file
extension should be .cppand in compilation phase replace gcc with g++. To install
G++ compiler, execute the command sudo apt-get install g++
If you have any problems then share it through comments. One more thing, This
video might help you in running your first C program in Ubuntu 11.10(Ive recorded
it in Gnome Shell interface)

2.What is C++ Programming


C++ is an object oriented programming (OOP) language, developed by Bjarne
Stroustrup, and is an extension of C language. It is therefore possible to code C++
in a "C style" or "object-oriented style." In certain scenarios, it can be coded in
either way and is thus an effective example of a hybrid language.
C++ is a general purpose object oriented programming language. It is considered to
be an intermediate level language, as it encapsulates both high and low level
language features. Initially, the language was called 'C with classes as it had all
properties of C language with an additional concept of 'classes. However, it was
renamed to C++ in 1983.
It is pronounced "C-Plus-Plus."
C++ is one of the most popular languages primarily utilized with system/application
software, drivers, client-server applications and embedded firmware.
The main highlight of C++ is a collection of pre-defined classes, which are data
types that can be instantiated multiple times. The language also facilitates
declaration of user defined classes. Classes can further accommodate member
functions to implement specific functionality. Multiple objects of a particular class
can be defined to implement the functions within the class. Objects can be defined
as instances created at run time. These classes can also be inherited by other new
classes which take in the public and protected functionalities by default.
C++ includes several operators such as comparison, arithmetic, bit manipulation,
logical operators etc. One of the most attractive features of C++ is that it enables
the overloading of certain operators such as addition.
A few of the essential concepts within C++ programming language include
polymorphism, virtual and friend functions, templates, namespaces and pointers.

Вам также может понравиться