[C Language][Object] Compile C become object && link object to binary file (executable file)
To use basic compilation involving multiple files or object files, start with the following two source files:
one.c
#include <stdio.h>
void hello()
{
printf("Hello world!\n");
}
two.c
extern void hello();
int main()
{
hello();
return 0;
}
The following procedure illustrates a simple, multi-file compilation process in its most basic form.
Procedure 5.3. Compiling a Program with Multiple Source Files
Compile one.c into an executable with:
gcc -c one.c -o one.o
Ensure that the resulting binaryone.o
is in the same directory asone.c
.
Compile two.c into an executable with:
gcc -c two.c -o two.o
Ensure that the resulting binarytwo.o
is in the same directory astwo.c
.
Compile the two object filesone.o
andtwo.o
into a single executable with:
gcc one.o two.o -o hello
Ensure that the resulting binaryhello
is in the same directory asone.o
andtwo.o
.
Run thehello
binary, i.e.hello
.
Reference :
Chapter 5. Compiling and Building
Tenouk's C & C++ program examples, source codes and tutorials download page
0 意見:
張貼留言