Question

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

Was it helpful?

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...

OTHER TIPS

Compiling is generally thought of as turning source code into machine code, but not necessarily also linking the machine code to create a final executable. Building is a more general term to describe the whole process of creating the final executable. Building will involve both compiling and linking. If you aren't using the -c option, then the linking is done automatically, so this would be considered building, but compiling was also part of that process.

You may find the terms being used a bit loosely though.

When you compile a program, the compiler just checks if the file has any compile-time errors (such as syntax and semantic errors) in it. When you "build" the program, the compiler first checks for any errors in it, and then converts the source code into machine code (actual compilation) and creates an executable file in the process.

For your second question, yes gcc -o helloworld helloworld.c alone would be enough to "turn the source code into an executable".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top