头文件重复包含后会怎样
先看正常情况加防止头文件重复定义
head1.h
*******************************
#ifndef _HEAD1_H_
#define _HEAD1_H_
void printf_hello(void);
void printf_world(void);
#endif
*******************************
head2.h
-----------------------------
#ifndef _HEAD2_H_
#define _HEAD2_H_
void printf_good(void);
void printf_luck(void);
#endif
-----------------------------
main.c
#include "head1.h"
#include "head1.h"
#include "head2.h"
#include "head2.h"
int main(void)
{
return 0;
}
void printf_hello(void)
{
// printf("hello\n");
}
void printf_world(void)
{
// printf("world\n");
}
void printf_good(void)
{
// printf("good\n");
}
void printf_luck(void)
{
// printf("luck\n");
}
gcc -E main.c -o main.i
main.i
# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "main.c"
# 1 "head1.h" 1
*******************************
void printf_hello(void);
void printf_world(void);
*******************************
# 2 "main.c" 2
# 1 "head1.h" 1
*******************************
*******************************
# 3 "main.c" 2
# 1 "head2.h" 1
-----------------------------
void printf_good(void);
void printf_luck(void);
-----------------------------
# 4 "main.c" 2
# 1 "head2.h" 1
-----------------------------
-----------------------------
# 5 "main.c" 2
int main(void)
{
return 0;
}
void printf_hello(void)
{
}
void printf_world(void)
{
}
void printf_good(void)
{
}
void printf_luck(void)
{
}
再看一下不加防止头文件重复定义
//test.h
***************
void fun(void);
***************
//main.c
#include "test.h"
#include "test.h"
int main(void)
{
return 0;
}
//main.i
# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "main.c"
# 1 "test.h" 1
***************
void fun(void);
***************
# 2 "main.c" 2
# 1 "test.h" 1
***************
void fun(void);
***************
# 3 "main.c" 2
int main(void)
{
return 0;
}