In this project the Raspberrypi board is booted with Ubuntu and is remotely accessed in GUI mode with a windows7 PC. Once starts accessing the Raspberrypi the host OS has little significance and one will be doing things in the Ubuntu OS most of the time using the Linux commands. There are number of software in the Linux which can simulate the command line (TUI) running inside the GUI. The Ubuntu installed in the Raspberrypi uses software called LXterminal and RootTerminal for simulating the command line.
The software can be selected and run from the applications button at the lower left corner of the Desktop as shown in the following image.
The user can select the LXTerminal or Root Terminal software and if the LXTerminal is selected the window will open up as shown in the following image:
The string that can be read from the window has the following meaning. The “root” is the current user login and the “raspberrypi” is the computer name. The “/” followed by the “:” represents the enire filesystem and is called the “root directory”. The “home” is the folder where all the user data can be found inside their respective folders. The second slash represents the entire “home” folder and “pi” is the folder where the current user “pi”‘s data are stored. The string simply suggests that the user is there currently in the user folder named “pi”.
It is a better practice to keep all the programming trials inside a separate folder. A separate folder can be created using the ‘make directory’ command “mkdir”. The following command will create a new folder named raspberrypi_C.
One can confirm whether the folder is created or not by typing the ‘list directory’ command “ls” or with an option “ls --color”
As shown in the above figure the folders will be displayed in dark blue color, image files in pink color, executable files in green color, zip files in red color and normal text files in white color. The newly created folder “raspberrypi_C” is also displayed as marked in the image.
Using the ‘change directory’ command “cd” one can jump into any required folder. To enter the folder ‘raspberrypi_C’ the “cd” command can be used followed by the folder name as shown below:
Notice that the string displayed has now changed from “/home/pi” to “/home/pi/raspberrypi_C” which means that the user is currently inside the folder “raspberrypi_C”.
Now to type in the first c program on the Raspberrypi a text editor is required. There is a text editor which can be operated in the command line itself and which is used by the Linux programmers around the globe. The text editor software is called “VIM” and can be downloaded and installed into the Ubuntu using the following command
apt-get install vim
Before using this command make sure that the Raspberrypi board is connected to the internet.
To open a file for editing using the vim simply type the command “vim” followed by the file name. Actually any file can be opened using the vim and if the file with the provided name does not exist in the current folder the vim will create a file with that name and open it up for editing. To create a file named “hello_world.c” and open it for editing using the vim the following command can be used:
The command will open up a text editor window from the command line itself as shown in the following image.
This is the vim editor window in its ‘normal mode’. Any commands that can be passed on to the vim like saving a file, undo changes, exit from the vim etc. can be done with the ‘command mode’. To go to the command mode of vim simply press the “:”, type the required command which is specific for the vim editor and press enter.
Since the C coding is about to be written into it is recommended to turn the syntax highlighting on. The command for highlighting the syntax is “syntax on”. Anything other than the actual text including the commands typed will appear at the bottom of the window as shown in the following image.
To start typing the code the user should jump out of the ‘command mode’ and use the ‘insert mode’. To jump to ‘normal mode’ back from command mode or any other mode simply press ESC key. To enter the insert mode press the key “i”. In the insert mode the user can type in the required text very much like in any other text editor.
Now the user can start typing the first simple program for the Raspberrypi. The following is the image of the simple program which can display a text “HELLO WORLD” written in the vim editor.
If done with typing in the code the user can jump back to the normal mode using ESC key. The line number if required can be displayed along with the text by using the vim command “set nu” after entering the command mode using the “:” key.
To save the file use the command “w” and to quit the vim, use the command “q”. Use the command “q!” to quit without saving. The save and quit command can be used together as “wq” as shown in the following image which will quit the user from the vim back to the command line.
The user will be directed back to the command line where the command used to open the vim editor is also visible. Now to ensure that the file named “hello_world.c” has been created use the ‘list’ command “ls” again which will list the file named “hello_world.c” as shown below:
Now it’s time to compile the .c file into an executable file and there are number of compiler software that can run from the command line and ‘cc’ or ‘gcc’ are the commonly used for compilers for C programs.
To compile any .c file using ‘cc’ from the command line simply type “cc” followed by the file name, here the user can type
The ‘cc’ creates the executable file with a default file name called “a.out” but the required executable file name can be specified by using the option “-o" follows by the required file name. To compile the .c file “hello_world.c” to an executable file “hello_world” the following command can be used.
cc hello_world.c -o hello_world
If there were any error in the code it should have appeared in the command line, since there is nothing appears other than the usual string after done the compilation it can be assumed that the executable file is created successfully. To confirm use the list command again as shown in the following image:
The green colored file is the executable created by the software ‘cc’ from the .c file ‘hello_world’. To execute this file or any other file from inside the folder where the file exists use “./” followed by the executable file name. Here to execute the file named ‘hello_world’ use the following command.
The following image of the command line shows all the commands that have been used for creating a .c file, compiling it to generate an executable file and finally executing the executable file. The result of the program execution “HELLO WORLD” can be also be seen in the image.