Compiling and Executing C++ programs in Linux

  • 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

Lab Practice Task

 

#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

Search within CuiTutorial

Scroll to Top