Program compile and link in linux.
Most of system software provides the source
code and we can use compile to build it into the executable binaries. This blog
will introduce what is inside the compilation process.
Compile tools
On linux platform, gcc is the most wide
used tool for c, c++ and even java compiler.
You will use
#which gcc
to determine if gcc has been installed
here is a single c program:
to determine if gcc has been installed
Single file compilation
here is a single c program:
#include <stdio.h> void main(void) { printf("hello world!"); }
now we can compile the program and run it
[root@X001 cprogram]# ls hello.c [root@X001 cprogram]# gcc hello.c [root@X001 cprogram]# ls a.out hello.c [root@X001 cprogram]# ./a.out hello world!
or you may use 'gcc -o outputfile sourcefile' to assign an output executable file name
multiple files compilation
if we have more than one single source file, which is the most case in application development. source code A will refer to other files, when one of the files is changed, do we need to re-compile all of the related files?the answer is no.
we have two files
File:a.c #include <stdio.h> int main () { printf("this is from first file\n"); method(); } File:b.c #include <stdio.h> void method(void) { printf("this is from second file:\n"); }
now we will do
1. compile the individual source files into object files
2. link the object files into binary
[root@X001 cprogram]# ll total 8 -rw-r--r--. 1 root root 95 Apr 14 14:11 a.c -rw-r--r--. 1 root root 87 Apr 14 14:05 b.c ======compile the files here =========== [root@X001 cprogram]# gcc -c a.c b.c [root@X001 cprogram]# ll total 16 -rw-r--r--. 1 root root 95 Apr 14 14:11 a.c -rw-r--r--. 1 root root 1568 Apr 14 14:11 a.o -rw-r--r--. 1 root root 87 Apr 14 14:05 b.c -rw-r--r--. 1 root root 1504 Apr 14 14:11 b.o =======link the files here ============== [root@X001 cprogram]# gcc -o result a.o b.o [root@X001 cprogram]# ./result this is from first file this is from second file
if we change the print statement of the b.c to "this is the updated file" .
we only need re-compile the b.o file and then regenerate the executable binary.
No comments:
Post a Comment