linux中gcc工具使用笔记

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$ ls
hello.c  hello.h  init.c  init.h  main.c  test
honker@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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值