linux中gcc工具使用笔记

1.gcc编译、汇编、链接

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

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

main.c
#include 
#include 
#include "hello.h"
#include "init.h"

void aftermain(void)
{
        printf("\n");
        printf("<<<<<<<>>>>>>>>>\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;
}
hello.c
#include 
#include "hello.h"
int hello(int argc,char* argv[])
{
        int i;
        printf("hello world!\n");
        for(i=0;i

init.c
 
  
#include 
#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;
}
~     

hello.h
 
  
#ifndef __HELLO_H__
#define __HELLO_H__
int hello(int argc,char* argv[]);
#endif
 
 
  
init.h
#ifndef __INIT_H__
#define __INIT_H__
int init(int number);
#endif
~        
编译,汇编,链接结果是
 
  
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......


<<<<<<<>>>>>>>>>
............................
 
  
 
  
 the introduction of atexit() function
  #include 
 
  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
 
  void exit (int status);
 
  void _Exit (int status);
 
  #include
 
  void _exit (status);
 
  其中调用 _exit,_Exit 都不会调用终止程序
 
  异常终止也不会。

 
  





你可能感兴趣的:(linux/unix)