Some terms used to compile C files need clarification.

I am learning C with the GCC compiler and Geany (Arch Linux, if that matters). However, I see that compilation and word collection are used interchangeably in both Geany and the Internet. I ask you to clarify that the way I understand the compilation process is correct, because Googling just bothers me.

Let's 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 will create the helloworld.o file. Geany calls this compilation of the process, and the compiler says Compilation finished successfully.

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

I understand that the -c option creates an object file and that several of them can be linked together with libraries to create an executable file, but I'm confused about which script is being compiled and which is being created.

Also, if I had only one source file in the project, for example, one helloworld.c file, is gcc -o helloworld helloworld.c to turn the source code into an executable?

thanks

+4
source share
3 answers

To answer your second question: yes, gcc -o myprog myprog.c is ok. So gcc -o myprog *.c or gcc -o myprog foo.c bar.c baz.c

To answer your first question: technically speaking, there is no word "building" :) However, the words "building" and "compilation" can be used interchangeably to describe the whole process of creating the final executable from the source code.

In a more precise context, you would say:

  • preprocessor, when the preprocessor includes header files, extends macros, etc.
  • where the parser tokens the source text and creates a structured data model (the so-called abstract syntax tree) of the program stream.
  • compilation or compilation when the code generator crosses the AST and generates assembly code from it
  • when the compiler driver calls the assembler program, which turns the assembly text into the binary code of the object, and finally
  • binding or binding, when the compiler driver calls the linker to search for characters in libraries, fills in missing addresses, etc.

So, strictly speaking, only the 3rd small step is compilation; in addition, using the GNU toolchain and make, people tend to invoke the first four steps (creating an object file from the .c file of the source file) as a whole.

Read more about all this here ...

+3
source

Typically, compilation is seen as turning source code into machine code, but it is also not necessary to link the machine code to create the final executable. Building is a more general term for describing the entire process of creating the final executable. The building will include both compilation and layout. If you do not use the -c option, then the connection is automatic, so this will be considered a building, but compilation was also part of this process.

You can find terms that are used a little loosely though.

+2
source

When compiling a program, the compiler simply checks to see if there are any compile-time errors in the file (for example, syntax and semantic errors). When you β€œcreate” a program, the compiler first checks for any errors 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 will be enough to "turn the source code into an executable file".

0
source

Source: https://habr.com/ru/post/1438218/


All Articles