Thursday 25 May 2017

Compile And Run a C/C++ Code In Linux(Ubuntu)

Hello Guys !!! Hope You all are doing well.
Toady I am going to discuss a simple topic for newbie in Linux(Ubuntu) .
How can compile and Run a C or C++ program on Linux operating systems using bash Terminal application?


To compile a C or C++ program on any Linux distro such as Ubuntu, Red Hat, Fedora, Debian and other Linux distro you need to install:

  1. GNU C and C++ compiler collection
  2. Development tools
  3. Development libraries
  4. IDE or text editor to write programs


Step #1: Install C/C++ compiler and related tools
If you are using Fedora, Red Hat, CentOS, or Scientific Linux, use the following yum command to install GNU c/c++ compiler:
# yum groupinstall 'Development Tools'

If you are using Debian or Ubuntu Linux, type the following apt-get command to install GNU c/c++ compiler:
$ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev


Step #2: Verify installation
Type the following command to display the version number and location of the compiler on Linux:
  • $ whereis gcc
  • $ which gcc
  • $ gcc --version
Step #3: Compile the Program
Create a file called HelloInC.c using a text editor such as vi, emacs or joe or gedit as per your choice
/* Hello World in C Program */
#include<stdio.h>
main()
{
printf("Hello World in C!\n");
}
Use any one of the following syntax to compile the program called HelloInC.c:
cc program-source-code.c -o executable-file-name
OR
gcc program-source-code.c -o executable-file-name
OR
## assuming that executable-file-name.c exists ##
make executable-file-name
In this example, compile HelloInC.c, enter:
cc HelloInC.c -o demo
OR
gcc HelloInC.c -o HelloInC
OR
## assuming HelloInC.c exists in the current directory ##
make HelloInC
If there is no error in your code or C program then the compiler will successfully create an executable file called HelloInC in the current directory, otherwise you need fix the code.

Step #4 : Run or execute the program

Simply type the the program name:
$ ./ HelloInC
OR
$ /path/to/ HelloInC

Note:- Same way you can compile and run C++ programs. Just save your C++ file with extension .cpp and follow the same procedure. As for example
g++ HelloInCpp.cpp -o HelloInCpp
  • g++ is the invocation of the C++ component of GCC, the defacto compiler for C/C++ and whole host of other languages on the Linux platform. It's currently the only compiler capable of compiling the Linux kernel.
  • HelloInCpp.cpp is the c++ source file you wish to compile.
  • -o main specifies the name of the output file you wish to create once the source is compiled. The target source file and the target output file can be inverted if you wish, so
  • g++ -o HelloInCpp HelloInCpp.cpp is equally valid.
  • To then execute that program, you need to do ./ HelloInCpp in the terminal.

Some More Useful Tips for C/C++

How do I generate symbolic information for gdb and warning messages?
The syntax is as follows C compiler:
cc -g -Wall input.c -o executable

The syntax is as follows C++ compiler:
g++ -g -Wall input.C -o executable

How do I generate optimized code on a Linux machine?

The syntax is as follows C compiler:
cc -O input.c -o executable

The syntax is as follows C++ compiler:
g++ -O -Wall input.C -o executable

How do I compile a C program that uses math functions?
The syntax is as follows when need pass the -lm option with gcc to link with the math libraries:
cc myth1.c -o executable -lm

How do I compile a C++ program that uses Xlib graphics functions?
The syntax is as follows when need pass the -lX11 option with gcc to link with the Xlib libraries:
g++ fireworks.C -o executable -lX11

How do I compile a program with multiple source files?
The syntax is as follows if the source code is in several files (such as light.c, sky.c, fireworks.c):
cc light.c sky.c fireworks.c -o executable

C++ syntax is as follows if the source code is in several files:
g++ ac.C bc.C file3.C -o my-program-name

Thanks
Saurabh 
Happy Coding!!!!

1 comment:

  1. hello,
    i just want to say that you have clearly mention the process very clearly and i also found it is very much easy to understand. keep sharing.

    android app development company in chennai

    ReplyDelete

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...