1
2
3
4
5
6
7
8
9
extern  #endif
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#define    MONGOOSE_HEADER_INCLUDED
 
"C"  /*.................................
  */
 
#endif /* __cplusplus */
 
1
2
3
4
5
6
7
          do       x;
f();
g(){x=f();}

在file2.c中g()使用的x和f()是定义在file1.c中的。extern关键字表明file2.c中x,仅仅是一个变量的声明,其并不是在定义变量x,并未为x分配内存空间。变量x在所有模块中作为一种全局变量只能被定义一次,否则会出现连接错误。但是可以声明多次,且声明必须保证类型一致,如:

int  int  extern       // x equals to default of int type 0
f();
double  extern 
  * char char
1
( const 
  i);
char  print( void  * s);
extern
char
void

编译为:

_print_float
1
2
3
4
5
6
 
print( #endif C_HEADER
#define C_HEADER

相对应的实现文件为cHeader.c的代码为:

void  i)
printf ,i);
1
2
3
4
5
6
7
8
9
}
 
int  ** argv)
print(3);
0;
1
2
3
4
5
6
 
void  i);
 
1
2
3
4
5
6
7
8
std;
int       <<i<<endl;
1
2
3
4
5
6
  i);
int  ** argv)
print(3);
0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  void void extern       (*CFT) ( *, *); void  void n, //style of C
isort( size_t  sz,FT cmp); xsort( size_t  sz,CFT cmp); extern  ysort( size_t  sz,FT cmp);
 
const  const  //style of C++
ccomp( *, *); f( int       //but compare is style of C++
(v,sz,1,&compare);
(v,sz,1,&ccomp);           //but ccomp is style of C
}
"C" #define CPP_HEADER #include <iostream> void int

注意:typedef int (*FT) (const void* ,const void*),表示定义了一个函数指针的别名FT,这种函数指针指向的函数有这样的特征:返回值为int型、有两个参数,参数类型可以为任意类型的指针(因为为void*)。

最典型的函数指针的别名的例子是,信号处理函数signal,它的定义如下:

(*HANDLER)( HANDLER int 
1
int  (*)( int
(*