Link to generate an executable file

I will explain how to link and generate an executable file. Think of a link as creating an executable from an object file generated by compilation. Links have a little more versatility, such as creating a shared library, but for now, let's first learn the basics of creating an executable.

Use the gcc command to do the link. Specify the object file as an argument.

#Link and output the executable file
gcc -o output file name object file 1 object file 2 object file n

I will write a sample to generate an executable file "myapp" from "myapp.o" including the main function and my own libraries "mylib1.o" and "mylib2.o".

#Link and output the executable file
gcc -o myapp myapp.o mylib1.o mylib2.o

In another article, I will explain how to actually create a library, perform split compilation, link multiple object files, and create one executable file.

How to compile and link at once?

The following describes how to compile and link at the same time and execute the program.

What is the difference between an object file and an executable file?

The same is true in that both object files and executable files are written in machine language.

The object file is not an executable file yet, so it cannot be executed by itself.

Isn't the linker ld?

There is a question that the linker for linking is not "gcc" but "ld". Yes, the linker is "ld", which is called from "gcc".

What kind of processing is the link specifically?

Details will be described when explaining split compilation, but if you write a link simply, make sure that the function name and global variable name described in the object file are included in other object files. And it is the work of connecting.

Each object file has a function name or global variable name that you do not know if it exists in any object file other than your own.

It only has name information and does not know the address location in the program.

Creating an executable from multiple object files with a link means resolving names such as function names and global variable names as the actual addresses in your program.

Associated Information