• About WordPress
    • WordPress.org
    • Documentation
    • Support
    • Feedback
  • Log In
  • Register
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment
Cuitutorial
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment

Operating System

Home » Blog » Compiling and Executing C++ programs in Linux

Compiling and Executing C++ programs in Linux

  • Posted by saqib
  • Categories Operating System
  • Date November 1, 2022
  • Comments 7 comments
  • GNU C++ Compiler (g++) is a compiler in Linux which is used to compile C++ programs.
  • It compiles both files with extension .c and .cpp as C++ files.

 Installing g++ compiler

  • By default, g++ is provided with most of the Linux distributions.
  • Details of installed g++ compiler by command

g++ –version

  • If g++ is not installed on your system; then it can be installed by writing the following commands

sudo apt-get update

sudo apt install g++

Compiling C++ program

  • compile a C++ program using command

sudo g++ source-file.cpp

  • The above command will generate an executable file a.out.
  • -o option to specify the output file name for the executable.

sudo g++ source-file.cpp -o executable-file

 Running a C++ program
  • Execute the created executable file by using the following command:

./executable-file

Activity 1

  • Write a simple C++ program that displays a Hello World message and the then compile and execute this program using the above commands.
  • To write the program, we use the gedit text editor. The source code file is saved with .cpp extension. (gedit hello.cpp) ,(g++ hello.cpp –o hello)
  • Open the gedit editor and pass the name of the file (hello.cpp) to be created(./hello)

Command-line Arguments

  • Command line argument is a parameter supplied to the program when it is invoked.
  • It is mostly used when you need to control your program from outside.
  • Command line arguments are passed to the main () method.

Passing command-line arguments to a C++ program

  • To pass command line arguments, we typically define main () with two arguments:
  • First argument counts the number of arguments on the command line
  • Second argument is a pointer array which holds pointers of type char which points to the arguments passed to the program.
  • The syntax to define the main method is

int main (int argc, char *argv[])

  • Here, argc variable will hold the number of arguments pass to the program while the argv will contain pointers to those variables.
  • argv[0] holds the name of the program while argv[1] to argv[argc] hold the arguments.
  • Command-line arguments are given after the name of the program in command-line shell of Operating Systems.
  • Each argument separated by a space.
  • If a space is included in the argument, then it is written in “”.

Activity 2

Open gedit to write c++ program.

Compile the c++ program using g++ compiler.

 

Related Links  to Operating System topics

Operating system Course content

  • Operating System-Functions and History
  • Generations of Operating System
  • Functions of an Operating System
  • Components of Operating System
  • Types of Operating System
  • Services of Operating System
  • Properties of Operating System
  • Processes in Operating System
  • Process Scheduling in Operating System

Lab Practice Task

  • Introduction to Linux Ubunto
  • Installation with virtual Box
  • Writing Linux Commands
  • Navigation in File System and Directory Management in Ubunto using CLI
  • File Handling and I/O Redirection In Ubunto
  • File Access Permission in Linux
  • Text Processing Tools and Basic System Configuration Tools in Linux
  • Package Management in Linux
  • How to manage processes in Linux
  • Compiling and Executing C++ programs in Linux
  • System Calls
  • Introduction To Shell Programming 

 

#Operating System complete course #Operating System past paper #Operating System-project #Computer Science all courses  #operating system Problem with source code#University Past Paper #Programming language #Question paper #old paper #Operating System-Functions and History #Generations of Operating System #Functions of an Operating System #Components of Operating System #Types of Operating System #Services of Operating System #Properties of Operating System #Processes in Operating System #Process Scheduling in Operating System #Introduction to Linux Ubunto #Installation with virtual Box #Writing Linux Commands #Navigation in File System and Directory Management in Ubunto using CLI #File Handling and I/O Redirection In Ubunto #File Access Permission in Linux #Text Processing Tools and Basic System Configuration Tools in Linux #Package Management in Linux #How to manage processes in Linux #Compiling and Executing C++ programs in Linux #System Calls #Introduction To Shell Programming

  • Share:
author avatar
saqib

Previous post

Processes management in Linux
November 1, 2022

Next post

System Calls in linux
November 1, 2022

You may also like

Aging in operating system
6 January, 2023

Aging in operating system In Operating systems, Aging is a scheduling technique used to avoid Starvation. Fixed priority scheduling is a scheduling discipline in which tasks queued for utilizing a system resource is assigned each priority. A task with a …

Starvation in operating system
6 January, 2023

Starvation in operating system It is a problem when the low-priority process gets jammed for a long duration of time because of high-priority requests being executed. A stream of high-priority requests stops the low-priority process from obtaining the processor or …

Dead Lock in operating system
3 January, 2023

Dead Lock in operating system A Deadlock is a situation where each of the computer process waits for a resource which is being assigned to some another process. In this situation, none of the process gets executed since the resource …

    7 Comments

Leave A Reply Cancel reply

You must be logged in to post a comment.

admin@cuitutorial.com
Facebook-f Twitter Youtube Linkedin Instagram Stack-overflow Pinterest Github Quora Whatsapp
Courses
  • All Courses
  • Past Paper
  • Final year projects
  • Interview Questions
  • Contact
Important Pages
  • Privacy Policy
  • Terms of Service
  • Cookie Policy
Links
  • University Events
  • Team
Education & learning platform for All Computer science subjects
Final year projects
Past Paper
Interview questions
Programming, C/C++, Asp.net/MVC. Android, MySql, Jquery, Ajax, javascript, Php, Html5, Bootstrap4.
NTS, GAT, PPSC, FPSC

Copyright © 2021 | Cuitutorial