简体   繁体   English

如何为Arduino库使用包含保护?

[英]How can I use an include guard for an Arduino library?

Why is the message printed twice in Example.h? 为什么在Example.h中将消息打印两次? Shouldn't #pragma once prevent it? #pragma once是否应该#pragma once阻止它?

Example.h: Example.h:

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

Test.ino: Test.ino:

#include "Example.h"

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

Output: 输出:

$ ~/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.

Include guards prevent inclusion of the same header more than once into the same translation unit / code file. 包含防护可防止将同一标头多次包含在同一翻译单元/代码文件中。
Two files, each being compiled separatly, each including the header with the message, will each show the message when they are compiled. 两个文件,每个文件都是分开编译的,每个文件都包含带有消息的标头,每个文件在编译时都会显示该消息。

If you include the header with the include guard a second time into the same file, then it will nevertheless onyl show the message once for that file. 如果您再次将标头和包含保护的标头包含在同一个文件中,那么onyl仍然会对该文件显示一次消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM