linux中gcc工具使用笔记

来源:互联网 发布:excel导入mysql 编辑:程序博客网 时间:2024/05/01 12:04

1.gcc编译、汇编、链接

实例工程:main.c hello.c init.c hello.h init.h

命令执行:gcc main.c hello.c init.c -o test

<span style="font-size:18px;">main.c</span>
<span style="font-size:18px;">#include <stdio.h>#include <stdlib.h>#include "hello.h"#include "init.h"void aftermain(void){        printf("\n");        printf("<<<<<<<<aftermain>>>>>>>>>>\n");        printf("............................\n");        return;}int main(int argc, char * argv[]){        printf("===========main===========\n");        init(1234);        hello(argc,argv);        atexit(aftermain);        printf("......atexit......\n");        return 0;}</span>
<span style="font-size:18px;">hello.c</span>
<pre name="code" class="cpp"><span style="font-size:18px;">#include <stdio.h>#include "hello.h"int hello(int argc,char* argv[]){        int i;        printf("hello world!\n");        for(i=0;i<argc;i++)        {        printf("argv[%d]=%s\n",i,argv[i]);        }        return 0;}</span>

init.c
<pre name="code" class="cpp"><span style="font-size:18px;">#include <stdio.h>#include "init.h"const char ro_data[1024]={"This is readonly data"};static char rw_data[1024]={"This is readwrite data"};static char bss_data[1024]={};int init(int number){        printf("the input number is %d \n",number);        printf("ro_data:%x,%s \n",(unsigned int)ro_data,ro_data);        printf("rw_data:%x,%s \n",(unsigned int)rw_data,rw_data);        printf("bss_data:%x,%s \n",(unsigned int)bss_data,bss_data);        return number;}~     </span>

hello.h
<pre name="code" class="cpp"><span style="font-size:18px;">#ifndef __HELLO_H__#define __HELLO_H__int hello(int argc,char* argv[]);#endif</span>
 
<span style="font-size:18px;">init.h</span>
<pre name="code" class="cpp"><span style="font-size:18px;">#ifndef __INIT_H__#define __INIT_H__int init(int number);#endif~        </span>
编译,汇编,链接结果是
<span style="font-size:18px;">honker@ubuntu:~/workdir/demo_gcc$ lshello.c  hello.h  init.c  init.h  main.c  testhonker@ubuntu:~/workdir/demo_gcc$ ./test===========main===========the input number is 1234 ro_data:80487a0,This is readonly data rw_data:804a040,This is readwrite data bss_data:804a460, hello world!argv[0]=./test......atexit......<<<<<<<<aftermain>>>>>>>>>>............................</span>
</pre><pre code_snippet_id="434836" snippet_file_name="blog_20140725_3_3652015" name="code" class="cpp">
</pre><pre code_snippet_id="434836" snippet_file_name="blog_20140725_3_3652015" name="code" class="cpp">
</pre><pre code_snippet_id="434836" snippet_file_name="blog_20140725_3_3652015" name="code" class="cpp"><strong><span style="font-size:18px;"> the introduction of atexit() function</span></strong>  #include <stdlib.h>   void exit_fn1(void)   {   printf("Exit function #1 called\n");   }   void exit_fn2(void)   {   printf("Exit function #2 called\n");   }   int main(void)   {   /* post exit function #1 */   atexit(exit_fn1);   /* post exit function #2 */   atexit(exit_fn2);   return 0;   }

 
  输出:
 
  Exit function #2 called
 
  Exit function #1 called
 
  进程的终止方式:
 
  有8种方式使进程终止,其中前5种为正常终止,它们是
 
  1:从 main 返回
 
  2:调用 exit
 
  3:调用 _exit 或 _Exit
 
  4:最后一个线程从其启动例程返回
 
  5:最后一个线程调用 pthread_exit
 
  异常终止有3种,它们是
 
  6:调用 abort
 
  7:接到一个信号并终止
 
  8:最后一个线程对取消请求做出响应
 
  #include <stdlib.h?
 
  void exit (int status);
 
  void _Exit (int status);
 
  #include <unistd.h>
 
  void _exit (status);
 
  其中调用 _exit,_Exit 都不会调用终止程序
 
  异常终止也不会。

<span style="font-size:18px;"></span>
<span style="font-size:18px;"></span>


0 0