C ++包括警卫似乎没有工作 [英] C++ include guards do not seem to be working

查看:98
本文介绍了C ++包括警卫似乎没有工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从mbed C ++编译器收到的错误消息似乎表明存在故障,包括警卫。

I am getting error messages from the mbed C++ compiler that seem to be indicative of malfunctioning include guards.

在main.cpp中,我包含我的头文件如下:

In main.cpp, I include my header file as follows:

#include "mbed.h"
#include "sample.h"

这是我的sample.h:

This is my sample.h:

#include "mbed.h"

#ifndef STUFF_H
#define STUFF_H

/* LEDs */
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

/* Subroutines */
void sweepLEDs();
void pulseLEDs(int numPulses);
void clearLEDs();

#endif

在sample.cpp中,我包括sample.h如下:

In sample.cpp, I am including sample.h as follows:

#include "sample.h"

在main.cpp和sample.cpp中,我指的是变量 led1,led2,led3,led4 而没有声明他们。但是,编译器输出这些投诉:

In both main.cpp and sample.cpp, I am referring to variables led1, led2, led3, led4 without declaring them. However, the compiler is outputting these complaints:


符号led1多重定义(由sample.cpp.cpp.LPC1768.o和main。 cpp.cpp.LPC1768.o)。
...
符号led4多重定义(通过sample.cpp.cpp.LPC1768.o和main.cpp.cpp.LPC1768.o)。

" Symbol led1 multiply defined (by sample.cpp.cpp.LPC1768.o and main.cpp.cpp.LPC1768.o)." ... " Symbol led4 multiply defined (by sample.cpp.cpp.LPC1768.o and main.cpp.cpp.LPC1768.o)."

我的包含警卫是否写得不正确?或者还有其他问题吗?

Are my include guards written improperly? Or is there some other issue?

(供参考,这里是链接到mbed.h源

推荐答案

这个问题是对警卫所做的事情的误解。包含防护可防止编译器在同一翻译单元中再次看到相同的内容(对于相同的.cpp文件)。他们阻止单独的翻译单位看到相同的代码。

The issue is a misunderstanding of what include guards do. Include guards prevent the compiler from seeing the same content again in the same translation unit (for the same .cpp file). They do not prevent separate translation units from seeing the same code.

在你的头文件中,你定义(不只是声明)变量。因此,包含标题的每个翻译单元都会创建自己的变量副本。

In your header file, you define (not just declare) the variables. Therefore every translation unit which includes the header creates its own copy of those variables.

正确的方法是在.cpp文件中定义变量并仅声明它们在标题中(无论如何都应该包含防范,以防止多次包含在同一个翻译单元中)。

The correct way to do it is to define the variables in a .cpp file and only declare them in the header (the include guards should be there anyway, to prevent multiple inclusion in the same translation unit).

也就是说,在你的文件sample.h中,前缀您的变量与 extern 并删除初始化程序(因此它们仅被声明,未定义),并在相应的.cpp文件中定义它们(也是定义函数的文件) ),通过在标题中输入确切的定义。

That is, in your file sample.h, prefix your variables with extern and remove the initializer (so they are only declared, not defined), and define them in the corresponding .cpp file (the one where also the functions are defined), by putting the exact definitions from your header there.

在一个不相关的注释中,你应该把 #includembed.h<包含保护内部的sample.h中的/ code>,因为某些编译器会优化这些保护的编译速度,并且如果包含保护之外的材料,则优化不起作用。请注意,这不是一个真正的正确性问题(假设mbed.h也受到包含警卫的适当保护),但是编译性能问题。

In an unrelated note, you should put the #include "mbed.h" in sample.h inside the include guards, because some compilers optimize compile speed for such guards, and that optimization doesn't work if there's material outside the include guards. Note that this is not really a correctness issue (assuming mbed.h is properly protected by include guards as well), but a compiling performance issue.

这篇关于C ++包括警卫似乎没有工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆