Let's create a directory for our code

mkdir -p ~/make_tut/tut_1/ && cd ~/make_tut/tut_1/

(Just in case you are wondering -p stands for "parent" and is used to create parent directory if it is non existent yet)

and begin with "the code": hello_world.c

nano hello_world.c
#include <stdio.c>              //to use printf() function
int main(){
    printf("Hello world! \n");  // "\n" to add a newline for ease of reading
    return 0;
}

After saving the code file, the next obvious step is to get an executable. In order to build, the simplest way is

gcc -Wall -g -o hello_world_exc hello_world.c

By now, you should see a file named hello_world_exc in the same directory. If you execute this file by typing

./hello_world_exc

the terminal will print out a message of Hello world!

Now let's look under the hood of the magic spell gcc -Wall -g -o hello_world_exc hello_world.c. Here:

  • gcc indicates the compiler. Any thing after this is either flag (which starts with -), or parameter
  • -Wall is a flag, telling the compiler that you want to print out "all" the warnings ("W")
  • -g is another flag, telling the compiler to produce symbolic information for debugging. But that is another tutorial.
  • -o, with a name following it, is the instruction for output, in this case is a file named hello_world_exc
  • last but not least, hello_world.c is the source file

Congratulation, you have just make the first code yourself! We will dig into further details next tutorial.

results matching ""

    No results matching ""