Friday, November 16, 2012

Start learning C on Linux (Fedora)

We students learn c on windows . We create the executable file (.exe) using turbo c or gcc compiler that runs only in windows machine. Have you tried the c on Linux and generating executable file for Linux? Not yet then follow this article.
In this article you learn how to:
1. Install  linux (e.g fedora or ubuntu)
2. Install  gcc compiler in fedora
3. Compile the c program using gcc.
You will also understand what are the executable file in Linux.

Installing the Fedora
There are many other open source Linux based operation system like Ubuntu, Fedora, Debian but I have chosen Fedora. You can download the Fedora iso image file form this link Download Fedora 17. List of other open source OS are in this link Download list.  Burn the image file on DVD using poweriso or other software. If you have windows on your PC and you want to keep both operating system then you have to be careful. My best recommendation for installing fedora is just separated one drive minimum of 20 GB size just delete it and left it as unallocated. During installation process while selecting the drive select the option use free space
(Use free space. If you have unallotted space on your hard drive, all that space is used for Fedora installation .)
You can find the complete install guide form fedora documentation site here.

Install gcc compiler in fedora
Open the terminal window.Type the following command which is in bold letter.You must have the internet connection. Here dinesh is user name of my computer your may have different.
[dinesh@localhost ~]$ su
[root@localhost dinesh]$ yum install gcc-c++
It automatically download the gcc compiler and install in default path.

Your first Linux C program
In this example you start developing for Linux using c by writing, compiling, and running your first Linux program. Lets start form hello world example.
Open the text editor gedit form application list.
Here is the source code for the hello.c

int main()
printf("hello world");
return 0;
save it to you home directories or in your own directories but you have to give complete path name of file for compilation.

Now compile, link, and run your program.

[dinesh@localhost ~]$ hello hello.c
[dinesh@localhost ~]$ ./hello
hello world
[dinesh@localhost ~]$

What are the executable file in Linux
We have seen that in window executable file have the extension .exe. In Windows the files are identified by their file extension like .exe, .com, etc (and hence the viruses with .exe extensions run easily on windows system).
In GNU/Linux systems the file type is determined by its data context (a file is recognized by its data: a script begin with "#!/bin/sh", an MP£ file with "ID3", and MPEG file with 0xBA hex code... and so on). So the file extension become unusefull (because if I rename test.mp3 to test.exe or test.txt its data doesn't change, and remains always an MP3 file and it is recognized as MP3 file). (see the file command!)

Executables files are recognizable by their data, but they can be executable only if it's execution permission bit is set (do you know about permission bitmask???). If executable bit is set, and if the file is an executable recognized by the running kernel, the application starts.
In terminal if you enter the command "ls /bin" or "/usr/bin" you'll see no file extensions.

An executable run with the permission of the caller: so if you run an application as user, the application have the permission of the user. If the user is "root", the application can do anything root can do!

Then, certain executable can be run only with root permissions, because it may edit some configuration file or anything else. So the application can do its job only if the application was started by the administrator (root).

No comments:

Post a Comment