Start Programming in GNU/Linux

Audience

Basic Steps

Any program goes through an identical phase before its output is obtained. First, the source code is written in a file. Then, the source code is compiled using the language's compiler. The compilation step generates either errors or an executable. Some people call it the binary, but that is not very accurate, given the fact that even image files are binary. Linking and loading processes are also involved before the final executable is obtained, but we are not discussing about them in this text.

All these steps are performed by different tools (or programs) invoked using different commands from the terminal. Integrated Development Environments are also available that make the job easier by providing the user an integrated system to perform all these tasks.

The steps involved for programs of interpreted languages is different. It is discussed in a separate location.

Writing Programs

There are a wide variety of text editors in GNU/Linux. Try some of them and you will start getting comfortable with a couple of them. Most popular of them are [:UsingVi:vi] and emacs; they have a strong fan following and tales of editor wars behind them. There are command line editors (those that can be used from the terminal without opening a separate GUI window) like nano and ed. GUI editors like Gedit, Kate and KWrite are bundled with the Desktop Environment you have installed. No matter what you use, you have to have basic familiarity on how to use the terminal.

If you have been trained in Turbo C/C++, you might have cultivated the habit of using the non-standard header file conio.h. You should stop doing that. Functions like clrscr() and getch() are provided by conio.h. The ANSI c does not include conio.h, thus non-standard. If you want to clear the screen, you should instead use system("clear"). Another common practise is to make the main() function return void, that means writing the function as void main(). Though not harmful, this is in violation with standard C language. Make main() return int by writing int main(). This requires you to write a statement return 0; or something similar at the end of your program. In UNIX based systems, newline is an important character. It is a good practice to end your C program files with a newline. You can do that by pressing enter. An example that sums up everything written above :

   /* hello.c */
   #include<stdio.h>
   int main(){
   printf("Hello World!\n");
   return 0;
   }

Save your program. The above program is saved as hello.c

Compiling Programs

In Turbo C/C++, to run a program you write a code and then press F9, then the program is automatically compiled and run. The IDEs in GNU/Linux provide similar features but we are learning to use the basic tools of compilation and execution in this tutorial. After saving your program file (hello.c), you need to compile it manually. The standard compiler of GNU/Linux systems is called the GNU Compiler Collection (GCC), that can compile many languages including C. The usual process of compilation is:

  $ gcc hello.c

The above command has to be issued in the terminal (from the command line). The $ sign at the prompt means that the user issuing the command is a normal user. If you see # there instead, it indicates that the user issuing the command is root. Root is the super-user who has every privilages to make changes in the system.

After the above command, the compiler reads the program files and compiles it. If there are errors in the program, the compiler lists the errors to you on the screen and you have to remove errors from the program. If there are no errors, a file named a.out is created.

  $ ls
  a.out    hello.c

As shown above, the command ls lists files and directories under the current directory. The file a.out that is created after compilation is the executable of your source code (hello.c in this case).

In GNU/Linux systems, all files and directories have permissions and modes associated with them. There are three types of modes: read(r), write(w) and execute(x). These modes can be set for three categories of users: owner, group and others. Description of file modes is out of the scope of this article. However you need to change the mode of the executable file so that it can execute :) Enter the following in the command line:

  $ chmod a+x a.out

This gives execute (x) permission to all (a) users. You can check the modes/permissions of a file by issuing the following command:

  $ ls -l a.out
  -rwxrwxr-x 1 bibek bibek 7047 2008-04-04 04:28 a.out

Adding -l after ls produces the long listing of the file. The part -rwxrwxr-x in the output of the above command shows the modes/permissions associated with the file. You can easily break this sequence of letters into three groups: rwx, rwx and r-x. The first group of permissions is for the owner of the file, second one for the group the owner belongs to and the last one is for other users. This file can be executed by all users of the system, exactly what we intended with the command chmod a+x a.out.

If a.out sounds dull to you, you've got help.

  $ gcc -o hello hello.c

This command produces hello as the executable instead of a.out.

Note
That in linux, there is not .exe/.com extension. The executable program doesnot have an extension.

Common Problems::

Install the necessary development headers. Generally they come in the package libcx-dev where x is the version number of libc. If it is already installed, get help from your local expert - it might be a problem with the environment variables.

Executing Programs

After compilation, you are now all set to execute your program. In Windows systems, you might have seen files ending with .exe. In GNU/Linux systems, it isn't necessary for files to have any particular extension to be executable. The file a.out is as much executable as hello (both created in above examples) is.

Issue the following command in the terminal:

  $ ./a.out
  Hello World!
  $

Viola it works!

When you enter commands in the terminal, the shell of the Operating System searches for the executable of that command. The shell doesn't search everywhere in the system for those executables, but only at those places (paths) specified by the environment variable PATH. You can see your system's PATH in this way:

  $ echo $PATH
  /usr/local/bin:/usr/bin:/bin

When you want a.out or hello to do the expected job, you need to have them in one of the places specified in the variable $PATH. Another method to invoke them is by providing their full address. This is why in the above example, we prepended the executable's name with a ./ . The dot indicates the current directory, hence telling the shell to search for a.out in the current directory instead of the system's PATH.

Integrated Development Environments

If the comfort of using a graphical utility for writing, compiling and executing the programs all from the same place is too hard for you to abandon, you can use the IDEs made for GNU/Linux systems.

One of the popular ones is the Anjuta IDE. Using such IDEs isn't very difficult. In case of trouble, refer the documentation (the help manual) that comes with such tools or consult a local expert.

Eclipse is an excellent IDE but a bit resource hungry but based beautifully on plug-in architecture. Users can download plugin and use that to provide functionality needed for development in say a particular language. There exists Java, Perl, Python and C/C++ and lot of other plugins to help users work on particular environment. It is free as in beer to download and use. :)

Common Problems and Questions

TODO

Where to go from here

TODO

Further Reading

Lot of IDEs listed at [https://help.ubuntu.com/ubuntu/desktopguide/C/programming.html https://help.ubuntu.com/ubuntu/desktopguide/C/programming.html ]

Vi/Vim is a powerful text editor favored by many programmers. Get started on using Vi : UsingVi


StartProgramming (last edited 2008-04-09 17:06:52 by HimanshuChhetri)