头文件重复包含

50 篇文章 2 订阅

头文件重复包含后会怎样


先看正常情况加防止头文件重复定义

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值