Search code examples
cgcccompilationbuildinggeany

Need clarification about certain terms used to do with compiling C files


I am learning C with the GCC compiler and Geany (Arch Linux, if it makes a difference). However, I am seeing the words compile and build used interchangeably, both in Geany and on the internet. I am asking for clarification that the way I understand the compiling process is correct, because Googling it is just making me more confused.

Say I write a simple helloworld.c file:

#include <stdio.h>
int main(void)
{
    printf("Hello world!");
    return 0;
}

If I run gcc -c helloworld.c, the compiler produces a helloworld.o object file. Geany calls this process compilation and the compiler says Compilation finished successfully.

Now, if I run gcc -o helloworld helloworld.c, the compiler produces an executable file called helloworld and Geany calls it building. However, the compiler again says Compilation finished successfully.

I understand that the -c option produces an object file, and that multiple of these can be linked together with libraries to produce an executable file, but I am confused about which scenario is compilation and which is building.

Furthermore, if I had just one source file in the project, such as a single helloworld.c file, is gcc -o helloworld helloworld.c enough to turn the source code into an executable?

Thanks


Solution

  • To answer your 2nd question: yes, gcc -o myprog myprog.c is just fine. So is gcc -o myprog *.c or gcc -o myprog foo.c bar.c baz.c.

    To answer your first question: technically speaking, there's no word as 'building' :) However, the word 'building' and 'compiling' can be used interchangeably to describe the whole process of producing a final executable from source code.

    In a more precise context you would say there is:

    • preprocessing, when the preprocessor includes header files, expands macros, etc.
    • parsing, where the parser tokenizes the source text and produces a structured data model (a so-called Abstract Syntax Tree) of the program flow.
    • compiling or compilation, when a code generator traverses the AST and generates assembly code from it
    • assembling, when the compiler driver invokes an assembler program which turns the assembly text into binary object code and finally
    • linking or linkage, when the compiler driver invokes a linker to look up symbols in libraries, fill in missing addresses, etc.

    So, strictly speaking, only the 3rd small step is the compilation; furthermore, using the GNU toolchain and make, people tend to call the first four steps (producing an object file from a .c source file) compilation as one.

    More on all this here...