gcc编译器入门

Gcc编译流程:
  • 预处理(Pre-Processing);
  • 编译(Compiling);
  • 汇编(Assembling);
  • 链接(Linking);

以intime.c为例讲解:
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#define PROMPT "time is up to 2 seconds\n\a"
char *prompt=PROMPT;
unsigned int len;
void prompt_info(int signo){

 write(STDERR_FILENO,prompt,len);

}

void init_sigaction(void){
 struct sigaction act;
 act.sa_handler = prompt_info;
 act.sa_flags=0;
 sigemptyset(&act.sa_mask);
 sigaction(SIGPROF,&act,NULL);
}

void init_time(){

 struct itimerval value;
 value.it_value.tv_sec =2;
 value.it_value.tv_usec=0;
 value.it_interval=value.it_value;
 setitimer(ITIMER_PROF,&value,NULL);

}

int main(){
 len=strlen(prompt);
 init_sigaction();
 init_time();
 while(1);
 exit(0);
}


1)预编译阶段
在该阶段,编译器将代码中的头文件编译进来,并且用户可以使用Gcc的选项“-E”进行查看,该选项的作用是让Gcc在预编译后停止编译过程。
执行命令:
gcc -E intime.c -o intime.i

下面是部分intime.i的代码:
……
extern char *strsignal(int);
extern int ffs(int);
extern int strcasecmp(const char *, const char *);
extern int strncasecmp(const char *, const char *, size_t);
extern size_t strlcpy(char *, const char *, size_t);
extern size_t strlcat(char *, const char *, size_t);





extern char *strdup(const char *);
# 6 "intime.c" 2

char *prompt="time is up to 2 seconds\n\a";
unsigned int len;
void prompt_info(int signo){

 write(2,prompt,len);

}

void init_sigaction(void){
 struct sigaction act;
 act._funcptr._handler = prompt_info;
 act.sa_flags=0;
 sigemptyset(&act.sa_mask);
 sigaction(29,&act,0);
}

void init_time(){

 struct itimerval value;
 value.it_value.tv_sec =2;
 value.it_value.tv_usec=0;
 value.it_interval=value.it_value;
 setitimer(2,&value,0);

}
int main(){
 len=strlen(prompt);
 init_sigaction();
 init_time();
 while(1);
 exit(0);
}

由此可见,gcc确实进行了预处理,它把“stdio.h”等头文件的内容插入到intime.i中。


2)编译阶段
接下来进行的是编译阶段,在这个阶段中gcc首先检查代码的规范性、语法错误等,以确定代码的实际要做的工作,在检查无误后,gcc把代码翻译成汇编语言。用户可以使用“-S”选项来进行查看,改选项只进行编译而不进行汇编,生成汇编代码。
//home/l/g/tomotoboy/c >gcc -S intime.i -o intime.s

现在让我们来看看intime.s的部分代码
/home/l/g/tomotoboy/c >cat intime.s
        .file   "intime.c"
        .section        .rodata
.LC0:
        .string "time is up to 2 seconds\n\007"
.globl prompt
        .data
        .align 4
        .type   prompt, @object
        .size   prompt, 4
prompt:
        .long   .LC0
        .text
.globl prompt_info
        .type   prompt_info, @function
prompt_info:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        subl    $4, %esp
        pushl   len
        pushl   prompt
        pushl   $2
        call    write
        addl    $16, %esp
        leave
        ret
        .size   prompt_info, .-prompt_info
.globl init_sigaction
……

……
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        addl    $15, %eax
        addl    $15, %eax
        shrl    $4, %eax
        sall    $4, %eax
……
这也是一个相当长的文件。

3)汇编阶段
汇编阶段是把编译阶段生成的".s"文件转成目标文件,我们使用选项为"-c"就可以了看到汇编代码已转化为".o"结尾的二进制木标代码了,如下:
/home/l/g/tomotoboy/c >gcc -c intime.s -o intime.o
/home/l/g/tomotoboy/c >ls
Makefile     factorial.o  intime.i     intime.s     main.c
factorial.c  intime.c     intime.o     main         main.o


4)链接阶段
在成功编译之后,就进入链接阶段。在这里涉及到一个重要的概念:函数库。函数库一般分为静态和动态库两种。
静态库是指编译链接时,把库文件的代码全部加到可执行文件中,因此生成的文件比较大,但在运行时就不再需要这个文件了。其后缀名一般为".a"。动态链接库与之相反,在编译链接时并没有吧库文件的代码加入到可执行文件中,而在程序执行时由运行时链接文件加载库,这样可以节省系统开销。动态链接库一般为".so"。如libc.so.6就是动态库。gcc在编译时默认使用动态库。
/home/l/g/tomotoboy/c >gcc intime.o -o intime
/home/l/g/tomotoboy/c >ls
Makefile     factorial.o  intime.c     intime.o     main         main.o
factorial.c  intime       intime.i     intime.s     main.c

我运行一下
/home/l/g/tomotoboy/c >intime
time is up to 2 seconds
time is up to 2 seconds
time is up to 2 seconds
time is up to 2 seconds
time is up to 2 seconds

你可能感兴趣的:(C++,c,gcc,C#,UP)