我如何为 Arduino 库使用 include guard?

How can I use an include guard for an Arduino library?

为什么消息在Example.h中打印了两次?不应该 #pragma once 阻止它吗?

Example.h:

#pragma once
#pragma message "Included"
Example.cpp:
#include "Example.h"

Test.ino:

#include "Example.h"

void setup() {}
void loop() {}

输出:

$ ~/Repositories/arduino-1.8.7/arduino --board arduino:avr:mega:cpu=atmega2560 --verify test/Test.ino`
Picked up JAVA_TOOL_OPTIONS: 
Loading configuration...
Initializing packages...
Preparing boards...
Verifying...
In file included from /home/ToBeReplaced/Test/test/Test.ino:1:0:
/home/ToBeReplaced/Arduino/libraries/example/Example.h:2:17: note: #pragma message: Included
 #pragma message "Included"
                 ^
In file included from /home/ToBeReplaced/Arduino/libraries/example/Example.cpp:1:0:
/home/ToBeReplaced/Arduino/libraries/example/Example.h:2:17: note: #pragma message: Included
 #pragma message "Included"
                 ^
Sketch uses 656 bytes (0%) of program storage space. Maximum is 253952 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 8183 bytes for local variables. Maximum is 8192 bytes.

包含守卫防止将相同的 header 多次包含到同一个翻译单元/代码文件中。
两个文件,每个文件都单独编译,每个文件都包含 header 和消息,每个文件在编译时都会显示消息。

如果您将 header 和包含防护第二次包含到同一个文件中,那么它仍然只会为该文件显示一次消息。