#ifndef #define #endif #ifdef 避免重复引用

12 篇文章 0 订阅

一:在什么阶段处理 ?

预处理 预处理 预处理

首先注意这四个头文件保护符是在预处理阶段由系统默认的预处理器(Linux操作系统上默认是cpp)来处理的。它们的含义如下:

#define XXX    // 将XXX声明为一个预处理变量
#ifndef XXX    // 当且仅当XXX没有被定义为"真"
#ifdef XXX     // 当且仅当XXX被定义时为"真"
#endif         // 遇到endif结束

二:怎么使用?

给个重复定义的例子如下所示:

A.h

class A {
public:
    A() = default;
    A(int i) {
        id = i;
    }
    int id;
};

B.h

class A {
public:
    A() = default;
    A(int i) {
        id = i;
    }

    int id;
};

main.cpp

#include<iostream>
#include"A.h"
#include"B.h"

int main(int argc,char *argv[])
{
    A a(9);
    std::cout << a.id << std::endl;
    return 0;
}

编译报错如下所示:

这里写图片描述

它提示”class A”被重复定义了,既然它的处理过程是在预处理阶段,那我们可以gcc -E生成预处理之后的文件看看。

$ g++ -E main.cpp -o main.i

看看main.i的内容,发现果然class A的内容被引入了两份

这里写图片描述

在编译的时候就会报错(cc1是Linux下默认编译器):

这里写图片描述

三:怎么改?

很容易,使用头文件保护符就好了。使用套路如下所示:

#ifndef  XXX   //如果没有定义XXX
#define  XXX   //那就定义XXX,直到遇到endif

#endif         //遇到endif表示结束定义

去修改我们的A.hB.h如下所示:
这里写图片描述

之后再次预处理下看看:

这里写图片描述

发现class A只有一份了吧!并且编译正常通过。

四:总结

其实这几个头文件保护符应该作为我们的一种编程习惯,而不是在发生冲突的时候才使用。这样保证了我们工程的健壮。至于它们别的作用,条件编译等等,也是对这几个符号的属性不同的利用场景罢了,我就不赘述了。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
/*** *eh.h - User include file for exception handling. * * Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved. * *Purpose: * User include file for exception handling. * * [Public] * ****/ #if _MSC_VER > 1000 #pragma once #endif #ifndef _INC_EH #define _INC_EH #if !defined(_WIN32) && !defined(_MAC) #error ERROR: Only Mac or Win32 targets supported! #endif #ifdef _MSC_VER // Currently, all MS C compilers for Win32 platforms default to 8 byte // alignment. #pragma pack(push,8) #endif // _MSC_VER #ifndef __cplusplus #error "eh.h is only for C++!" #endif /* Define _CRTIMP */ #ifndef _CRTIMP #ifdef _DLL #define _CRTIMP __declspec(dllimport) #else /* ndef _DLL */ #define _CRTIMP #endif /* _DLL */ #endif /* _CRTIMP */ /* Define _CRTAPI1 (for compatibility with the NT SDK) */ #ifndef _CRTAPI1 #if _MSC_VER >= 800 && _M_IX86 >= 300 #define _CRTAPI1 __cdecl #else #define _CRTAPI1 #endif #endif typedef void (__cdecl *terminate_function)(); typedef void (__cdecl *unexpected_function)(); typedef void (__cdecl *terminate_handler)(); typedef void (__cdecl *unexpected_handler)(); #ifndef _MAC struct _EXCEPTION_POINTERS; typedef void (__cdecl *_se_translator_function)(unsigned int, struct _EXCEPTION_POINTERS*); #endif #if _MSC_VER >= 1200 _CRTIMP __declspec(noreturn) void __cdecl terminate(void); _CRTIMP __declspec(noreturn) void __cdecl unexpected(void); #else _CRTIMP void __cdecl terminate(void); _CRTIMP void __cdecl unexpected(void); #endif _CRTIMP terminate_function __cdecl set_terminate(terminate_function); _CRTIMP unexpected_function __cdecl set_unexpected(unexpected_function); #ifndef _MAC _CRTIMP _se_translator_function __cdecl _set_se_translator(_se_translator_function); #endif #ifdef _MSC_VER #pragma pack(pop) #endif // _MSC_VER #endif // _INC_EH

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨博东的博客

请我喝瓶可乐鼓励下~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值