Pages

Search my Blog!

Monday, May 31, 2010

How to Compile C program from Ubuntu Terminal

It is really very easy to do that. Just follow these steps:
1) Open the terminal: Go to Applications => Accessories => Terminal
2) Create folder in home directory (It is always a good practice to save your programs in one folder. You may create it anywhere for your convenience)
Type the following in your terminal to create folder then press enter key

mkdir cprograms

Notes:
  • cprogram is the name of your folder, replace it with the name you like!
  • Skip to step three if you have your folder ready

3) Change directory to this folder you have created. Type the following and then press enter

cd cprograms

4) Create the source file. This is the file containing your source code. Suppose our file is named hello.c

gedit hello.c

5) In the editor that opened, type your program. Suppose the program is to display Hello World! on the screen, you type the following

#include
int main(){
   printf("Hello World!\n");
   return 0;
}

6) Save and close your editor
7) Compile your program: Type the following then press enter

gcc -o hello hello.c

Notes
  • gcc is GNU C Compiler, it is shifted with ubuntu, and get installed automatically once you install ubuntu in your box
  • hello is the executable created after successfully compilation
8) Run your program. Type the following and then press enter

./hello

9) you will see your program output displayed on your terminal. In our example "Hello World!"

That is all you can do to create and run your program.

Saturday, May 22, 2010

Installing Sun’s JAVA on Ubuntu Lucid Lynx (10.04)

I successful installed Sun's JAVA on Ubuntu 9.04 (Jaunty Jackalope) and later, on Ubuntu 9.10 (Karmic Koala) I upgraded from 9.04. With introduction of Ubuntu 10.04 (Lucid Lynx), I decided not to upgrade but freshly installed it.
When I tried to install Sun (Oracle) Java, through apt-get, I got the error message saying package (sun-java6-jdk) is missing. After researching on net, I found that since Lucid Lynx, Ubuntu opted for OpenJDK, which I think is a good I idea. But what about those previous programs that explicitly need Sun Java? What about those who are using Sun Java to learn Java programming as part of their curriculum? Good news, Ubuntu still shipping Sun Java, but in partners section. So you need to add that section to your sources and install Sun's JDK as follows:

#sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
#sudo apt-get update
#sudo apt-get install sun-java6-jdk

You will need to accept the license agreement.

You may also need to install the java plugin for firefox. This is done as follows:

#sudo apt-get install sun-java6-plugin

After installing, your Ubuntu has two jdk, OpenJDK and Sun's JDK. If you want to use Sun's JDK instead of OpenJDK (which is the default option) type the following command:

#sudo update-alternatives --config java


A window will open showing your installed jdks, from there, you can change to your preferred jdk.

Next, I will write to you the difference between OpenJDK and Sun JDK.

Any comment? Any question? Welcome again.