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;
}
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">
</span>