Compile C language source code

Let's compile the C language source code. This is a pure compilation that produces an object file from C source code.

Think of an object file as a part of a program written in machine language.

Compiling is the process of converting a C language source file written in text into an object file written in machine language.

Use the "-c" option to compile.

#Compile C language source files
gcc -c input source code file name

Let's create an object file called "test.o" from a file called "test.c".

// test.c

#include <stdio.h>

int main (void) {
  printf("Hello World! \ N");
}

Execute the gcc command. An object file called "test.o" will be generated.

gcc -c test.c

If you want to specify the output file name yourself, you can use the "-o" option.

gcc -o test2.o -c test.c

Link to generate an executable file from the object file

Let's generate an executable file from one object file. This work is called a link, but it doesn't feel like a link because it creates one executable file from one object file.

Generate the executable file "test" from the object file "test.o" generated earlier.

#Generate an executable file from an object file
gcc -o test test.o

Let's check if "test" can be executed as an executable file.

./test

Can one executable file be generated from multiple object files?

Yes. You can generate one executable file from multiple object files.

gcc -o test test.o foo.o bar.o

I'll do this in the discussion of split compilation.

Is there a compiler other than gcc?

Think of gcc as the default C language compiler for UNIX / Linux created by the GNU Project.

There are C language compilers made by other manufacturers, which may be superior in terms of compilation speed, link speed, and execution speed.

I think gcc is excellent in terms of portability in UNIX / Linux and its affinity for open source culture.

In the case of corporate products, the advertising message is often exaggerated, and even if the original product can be used sufficiently for its intended purpose, increasing corporate control may be the motivation for development. Since there is, I think it is better to subtract that point and perform quality evaluation.

Associated Information