Tuesday, February 5, 2008

Hello World C Application On Ubuntu

Working with PHP has been a good way for me to become more familiar with C programming. It seems that alot of the syntax carries over between the two. Same with standard function names, like fopen for instance.
My biggest issue with picking up C was that I always became fustrated with trying to setup and understand compilers & the tools that come with them. I couldn't even get a Hello World application written in C to work.

I decided to give C Programming another try on my Ubuntu box. All of my previous attempts had been on Windows, and now that I've become familiar with Ubuntu it seemed like somthing worth trying.

It was actually alot easier than I thought it was going to be. Since I don't have a monitor hooked up to my Ubuntu computer I used PuTTy to run commands, and FTP for file editing.

First thing I did was use PuTTy to create a foo directory in my home directory, then I put some files in the foo directory.
me@box:~$ mkdir /home/me/foo
me@box:~$ cd /home/me/foo
me@box:~/foo$ touch .deps
me@box:~/foo$ touch Makefile
me@box:~/foo$ touch hello.c


I opened hello.c in my text editor, and typed the following.
#include <stdio.h>

main()
{
puts("Hello World!");
return 0;
}


After I saved hello.c and placed it on the server with FTP I ran the following command.
me@box:~/foo$ make hello

Which output the following before going back to the prompt.
cc     hello.c   -o hello


That's it. I ran the application from the prompt & out popped "Hello World!".
me@box:~/foo$ ./hello
Hello World!
me@box:~/foo$

1 comment:

Anonymous said...

Thanks, just what I was looking for