[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.ois 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.ois in the same directory astwo.c.
Compile the two object filesone.oandtwo.ointo a single executable with:
gcc one.o two.o -o hello
Ensure that the resulting binaryhellois in the same directory asone.oandtwo.o.
Run thehellobinary, i.e.hello.
Reference :
Chapter 5. Compiling and Building
Tenouk's C & C++ program examples, source codes and tutorials download page
0 意見:
張貼留言