Tuesday 24 November 2015

Compiling and Running C programs in Ubuntu command line

 


Many of you might be knowing the C language thoroughly, but have you ever tried to run the very basic programs in the Linux through command line? You may not have done that, like me.

I had learned C programming using Borland C in the windows about 11 years ago, and then used various IDEs to compile and run the programs  for different platforms.


But having studied in India, you would hardly think of using Linux as an operating system during your student time, I hope many of you would relate to this. But anyway, nothing is late if you want to learn something.

Being familiar with Linux will give you a lot of advantages, including ample amount of open source tools available for development of anything related to electronics or computer science.

Let's have a look, how easy is to install a C compiler and start running your first program in Linux.

I am using Ubuntu distribution as it is the most user friendly and closest to Windows version of Linux.


Installation of Packages


For Debian based systems like Ubuntu, you need to install readily available "build-essential" packages using below command from your terminal.


$ sudo apt-get install build-essential

If you are using RPM based system like Fedora and RedHat, you can install it by below command from terminal.


$ su -c "yum groupinstall development-tools"

You will be asked to provide your user account's password for Linux system.


Text Editor


We have been using Notepad in the Windows since the beginning mostly. But Linux has multiple good options available.

The Gedit is the closest to Notepad in Windows for writing programs in.

There are other options available like Vim, Emacs and Nano. But leave it for later.

Now, we will create our first C file as we did in Windows. THE HELLO WORLD PROGRAM.

You just need to type a below command in your terminal.

$ gedit hello.c

Then enter the below program in your hello.c file, save it and close it.


1
2
3
4
5
6
int main(int argc, char *argv[])
{
    puts("Hello world.");

    return 0;
}



Compiling the C program


We are using compiler cc (C Compiler) to compile our C program.

So the command is so simple.


$ cc hello.c -o hello

Let's understand the command.
By typing cc we are invoking C Compiler then we have provided C file named hello.c and -o defines the output/object file which will be generated named hello.

Now using ls you can check that there will be two files in your directory. One is hello.c which we have created and another is hello which is created by our cc compiler.


Execute the Object file


To execute the file in Borland C we used F7 key to build and to run we used F5 key. But here we need just a simple command ./<filename>.

So in terminal we will write below command and see the output.


$ ./hello
Hello world.


Debugging C Program


We have just compiled a program with default settings and the output was perfect.

But now we will compile the program with all warnings on. We will add -Wall option in the previous command which will do work we want.


$ cc -Wall hello.c -o hello
hello.c: In function ‘main’:
hello.c:3:5: warning: implicit declaration of function ‘puts’ [-Wimplicit-function-declaration]
     puts("Hello world.");
     ^

Now you can see there is a warning of implicit declaration for function "puts". You should ideally get rid of all the warnings for your C program.

In this case you can eliminate warning by including proper header file by adding it to top of program and recompile it.


#include <stdio.h>

Did you tried to run it again? The warning would have gone. Keep practicing with this bare minimal way to programming.

Like the content? Like our facebook page and spread the word.

No comments:

Post a Comment