在一个头文件中声明的函数
//head.h
#ifndef __HEAD_H__
#define __HEAD_H__
extern void fun();
#endif
的作用是想让其他有 #include "head.h"的文件都有 extern void fun();这句话。这样的话,在连接的时候就会自动去找到fun函数的实现.因为对于函数来说:
void fun();
这样也算是声明。所以我认为 这里写不写extern都可以。但是如果是变量就不同了。
//head.h
#ifndef __HEAD_H__
#define __HEAD_H__
extern int i;
#endif
//test.cpp
#include "head.h"
int i=3;
//main.cpp
#include "head.h"
#include <iostream>
using namespace std;
//如果head.h里面没有写 extern int i;
//在这里添加也一样
//extern int i;
int main()
{
cout<<i<<endl;
return 0;
}