DEV Community

David Mebo
David Mebo

Posted on

Rough intro to GCC (Hello, World!)

Write a script that runs a C file through the preprocessor and save the result into another file.
The C file name will be saved in the variable $CFILE
The output should be saved in the file c

That's the first task starting my journey learning C, aside setting up your environment and all. Seems pretty straightforward... till I keyed in man gcc in the terminal. The output? Lets just say I read half-way and gave up. Ended up getting a book on GCC which in my opinion did a better job breaking down the commands and flags eli5 style.

Anyways, C programs go through 4 compilation stages;

  • Preprocessor
  • Compiler
  • Assembler
  • Linker

Preprocessing

In the preprocessing stage, macros and header files are expanded i.e. you see what you added to the files. Macros? Header files? You see their source codes.
By the way, the preprocessing step creates a file with .i extension. To pass a c file through a preprocessor, the flag you are looking for is -E, command;

gcc -E foo.c

Compilation

Next step is the compilation stage. Here, the preprocessed file is passed through a compiler, which in turn creates source file with an extension '.s', which contains assembly code. The flag here is -S, same command used as the preprocessor but the flags are swapped. Do note though that using the flag will not create an object file, that's the job of the assembler.

Assembler

Third stage, the assembler. This is where assembly code goes from barely understandable except by those who learnt about it before I was born to being unreadable by virtually anyone on this planet, unless you are a robot or someone who's dedicated their life following the ways of the machines.
The assembler takes the source file and turns it to an object file which contains machine code. To invoke the assembler, use

as foo.s -o foo.o

This will generate the object file 'foo.o'.

Linker

The final stage is the linker. There's a lot going on under the hood here but the short of it is you invoke gcc followed by the object file one wishes to compile, like this;

gcc foo.o

The command generates an executable called a.out. To run the executable, point to the path where the executable is located, followed by the executable name on your terminal. Hit enter and the results of your C file should display on your terminal.

Back to the question, following the instructions, you'll realize you need two commands/flags. One for passing the file through a preprocessor, another for naming the output of the file
In summary, the answer's;

gcc -E $CFILE -o c

Hopefully the rest of the task makes perfect sense to you (except the advanced task maybe, had to check the man page and wikipedia to figure it out).

P.s: Blog post(s) are kinda like a journal to me and I am not in any way looking to educate anyone. It's not in me. Besides, there are lots of materials out there (I will gladly share the ones I used, they'd be mostly books, the occasional diving into the man pages and maybe stackoverflow and stackexchange).

Top comments (0)