Monday, July 19, 2010

Setting up

Before doing anything, you'll need to download and install some packages so that you have tools for your development.

Compiler

Basically, the only simple step you have to do is type in the Terminal:

sudo apt-get install build-essential

Hit Enter, type in your password and let it run. The command is applied for Ubuntu Linux and every other Debian-based distros. If your distro is not Debian-based, I am sure there is a equivalent package for it; go download and set it up.

For those who curious, this package is a set of tools considered essential for building software (Exactly it is the GNU C Compiler (GCC( and its dependencies)

Get a text editor

There are tons of decent text editor available for Linux user; However, in this series we are focusing on programming, so we will forget about choosing between different text editors for a while. Instead, we will use the text editor comes with Ubuntu and many other Linux distros - GEdit.

Believe me, GEdit is very cool, especially after we give it some power-up. We do this by typing in the Terminal:

sudo apt-get install gedit-plugins

Again, this command is Debian-based specific, but there is equivalen for other distros. Open GEdit and then you may ask me, what's up? I don't see the differences.

Open Edit->Preferences, disable text wrapping, enable line numbers, right margin and highlight matching bracket. These option is not compulsory, but they are very helpful.

  • Wrapping for long lines is easier for read, but for beginners, it bring difficulties to distinguish the statements.
  • Line numbers comes in very handy when you need to switch between different locations in the source code.
  • Next, about the right margin. You will see long lines of code is particularly hard to see, especially when line wrapping is disabled as I said above, right? Instead of rely on the text editor breaking the line randomly for us, we do it manually at column 80 following the right margin. This is a good practice followed by most programmer in the world (but our professors may not do so I guess :D).
  • Finally, highlight matching bracket options help you to ensure that you closed all the opening ones, reducing the error rate.

Next go to Editor, set tab width to 4 as it is the standard, and enable automatic indentation to save your time coding.

In Font & Colors you can change the color scheme to fit your taste.

So that's it! Your working environment is set and we're ready to go.

Your very first C program

Well, I don't want to start it now, as you will have some time to prepare your mind. However, we need to write a program, compile it and run it to ensure that our setup is OK.

Let's start of by creating a new Folder for the first program. You may want to place it in a folder for the whole series, such as [Your Home]/Study/LearnC/Chapter-1/.

Create a new file, name it hello.c, then open it with GEdit and type in:

#include <stdio.h>

int main() {
    printf("Hello, world!\n");

    return 0;
}

Looks weird? No problem, we can deal with it later. Now let's just compile and run it by typing so in the Terminal (make sure you have changed to our working folder):

gcc -Wall -Werror -W -o hello hello.c
./hello

You should see the string "Hello, world!" to be printed out. So it worked!

Conclusion

We have worked hard today, so we should stop now. As the exercise, try to figure out what is the meaning of each line in the program, and what we have done in order to compile it. The answer of course is in the next chapter. See you later.

No comments:

Post a Comment