Home Home > 2013 > 10 > 15 > openSUSE and GCC part 2: compiling ‘Hello World’
Sign up | Login

Deprecation notice: openSUSE Lizards user blog platform is deprecated, and will remain read only for the time being. Learn more...

openSUSE and GCC part 2: compiling ‘Hello World’

October 15th, 2013 by

I really hope you readied last article ‘OpenSUSE and GCC part 1: getting started‘ or you understand basics and you have GCC (Only GNU C Compiler as GCC stands Gnu Compiler Collection) installed. This time we learn how to compile application called ‘Hello World’. It’s so popular application even wikipedia have article about it. If you are not in reading mood I’ll explain it in short.

Hello world

Hello world is small application that programmer tends to start with when learning new programming language. It’s just something that prints ‘Hello World’-string in console. It’s good starting point to learn new programming language as in compiling languages you get the idea how to make application run.
Github is the programming project hosting place that uses git. If you adventurous you can see all the language ‘Hello World’ programs from this Github page. Just click them around and see that every program has the same idea. If you have no clue what all this means don’t feel bad! I hope in some point of you openSUSE hacking time you probably get back there and understand value of that small application.
In C-Language ‘Hello World’ looks like this:

#include <stdio.h>

int main(void) {
printf("Hello World\n");
return 0;
}

Copy these sentences as they are to ‘helloworld.c‘ file with pico, nano, vim, emacs, gedit, kate or some other text editor. Pay some attention where you save that ‘helloworld.c’. Easiest place would be you home dirs root /home/’yourusername’/. I won’t tell you want all this C-language means if you are interested please read how to get along with C and you see how easy language C really is.

Compiling

If you have used PHP, Python, Bash or Perl (maybe Ruby) in other words some programming language that doesn’t need compiling you are point of life to take a deep breath. If you have ever touched any programming language you in my position when I was 16 and started typing in Borland Turbo C++ and take event deeper breath. If you have used Microsoft Visual Studio and which is incredible piece of IDE and you tell yourself you know something about programming you take mild breathe and zip of water. Only problem with IDEs is you never know how the magic happens behind the scenes. Specially Visual Studio is very very good make you application just compile and in most of the cases that is what you want right?
There is some cases you need to get in the bottom and understand basic of compiling process. I tell one example from my life. In some point of my life I was ext in Nokia (Way before it was Microsoft owned phone company) and their phone build scripts were in HP-UX or AIX (can’t remember anymore sorry). There were nearly ‘NaN’ of people who understand how these Makefile scripts worked (they were mostly very cryptic cross compile Makefile stuff) and they just saw my fellow ext-buddy sitting there behind his desk. They gave these scripts to this poor fellow and weeks he cried, moaned and felt pain in his brains from compiling extremely complicated application. In the end he did it. He made those build scripts to work because he understand how compiling and linking was actually made and if I’m not wrong he got work place from Nokia for years to come.
Now I Cut the sentimental crap and tell how to compile program. It’s easy go to directory you have created ‘helloworld.c‘ and write this in console

gcc -o helloworld helloworld.c

was it so hard? In setence ‘gcc‘ is GNU C Compiler and paramter ‘-o‘ is application binary name (choose wisely young badawan it will write over your files if have same file name).

Run program

Running the program isn’t hard at all. Stay in directory that program is and write to console

./helloworld
Hello World

Now you have compiled you first program. Next we look at RPM devel packages and using libraries and headers. Remember ‘./’ is very needed in front this example because you want to run application from your current location.

Both comments and pings are currently closed.

Comments are closed.