2012年6月15日 星期五

[C Language][Object] Compile C become object && link object to binary file (executable file)


[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




  1. Compile one.c into an executable with:


    gcc -c one.c -o one.o


    Ensure that the resulting binary one.o is in the same directory as one.c.




  2. Compile two.c into an executable with:


    gcc -c two.c -o two.o


    Ensure that the resulting binary two.o is in the same directory as two.c.




  3. Compile the two object files one.o and two.o into a single executable with:


    gcc one.o two.o -o hello


    Ensure that the resulting binary hello is in the same directory as one.o and two.o.




  4. Run the hello binary, i.e. hello.





Reference :



Related Posts:

0 意見:

張貼留言