定义如下几个文件
knight@weare:~$ touch main.c test1.c test1.c test2.c test2.h
knight@weare:~$ ls
Makefile main.c test1.c test2.c test2.h
knight@weare:~$
其中在test1.h中定义如下函数:
#ifndef __TEST1_H__
#define __TEST1_H__
typedef void (*test_handle)(void* param);
typedef struct _test_param_handle
{
test_handle handle;
void* param;
}test_param_handle_t;
void test_register(void);
#endif
~
在test1.c中定义如下:
#include "stdio.h"
#include "test2.h"
void print_user(int i);
void system_register_handle(test_handle handle,void *dat);
void test_register(void)
{
system_register_handle((test_hadle)print_user,NULL);
}
void system_register_handle(test_handle handle, void *dat)
{
table.handle = handle;
table.param = dat;
}
void print_user(int i)
{
printf("This is a C test example num=%d\r\n",i);
}
在test2.h 中定义如下
#ifndef __TEST2_H__
#define __TEST2_H__
#include "test1.h"
test_param_handle_t table;
#endif
在main.c中定义如下:
#include "test2.h"
int main(void)
{
int i=0;
test_register();
for(i=0;i<=10;i++)
{
table.handle(i*100);
}
return 0;
}
由于我是在linux下编写的,Makfile 按如下编写:
test: main.o test1.o
gcc -o test main.o test1.o
main.o: main.c test2.h
gcc -c main.c
test1.o: test1.c test1.h test2.h
gcc -c test1.c
编译运行后结果如下:
knight@weare:~$ make
gcc -c main.c
main.c: In function ‘main’:
main.c:9:17: warning: passing argument 1 of ‘table.handle’ makes pointer from integer without a cast [-Wint-conversion]
table.handle((int)(i*100));
^
main.c:9:17: note: expected ‘void *’ but argument is of type ‘int’
gcc -c test1.c
gcc -o test main.o test1.o
knight@weare:~$ ./test
This is a C test example num=0
This is a C test example num=100
This is a C test example num=200
This is a C test example num=300
This is a C test example num=400
This is a C test example num=500
This is a C test example num=600
This is a C test example num=700
This is a C test example num=800
This is a C test example num=900
This is a C test example num=1000