C++ 在 Arduino 草图中包含 .cpp 和 .h 文件的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21409042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
correct way to include .cpp and .h files in an Arduino sketch
提问by Madivad
First, the problem:
一、问题:
main sketch file:
主草图文件:
char foo; // required to clean up some other problems
#include <Arduino.h> // tried it in desperation, no help
#include "a.h"
void setup(){
Serial.begin(9600);
Serial.println("\nTest begins");
for (int num = -1; num < 1; num++){
Serial.print(num);
if (isNegative(num)){
Serial.println(" is negative");
} else {
Serial.println(" is NOT negative");
}
}
}
void loop(){}
// a.h
//啊
#ifndef H_A
#define H_A
boolean isNegative(int x); // Err#1
int anotherOdity();
#endif // H_A
// a.cpp
// a.cpp
#include "a.h"
int isNegative(int x){
Serial.println("I can't print this from inside my INCLUDE FILE"); //Err#2
if (x<0) return true;
return false;
}
int anotherOdity(){
char ch[5];
memcpy(ch,"1",1); //doesn't work, memcpy not declared // Err#3
}
The above, as is, doesn't compile and these are the errors I get:
上面的内容无法编译,这些是我得到的错误:
In file included from a.cpp:1:
a.h:4: error: 'boolean' does not name a type
a.cpp: In function 'int isNegative(int)':
a.cpp:4: error: 'Serial' was not declared in this scope
a.cpp: In function 'int anotherOdity()':
a.cpp:11: error: 'memcpy' was not declared in this scope
The first problem is the boolean type, seems to suffer from some name mangling that the Arduino environment does, but that is generally fixed by the char foo;
in the main file. And in certain situations, it is. But to use that type in the .cpp
file generates this error.
第一个问题是布尔类型,似乎受到 Arduino 环境所做的一些名称修改的影响,但这通常由char foo;
主文件中的修复。在某些情况下,确实如此。但是在.cpp
文件中使用该类型会产生此错误。
I can see that Errors 2 and 3 are related, but how do I get these in scope? I realise that part of the problem is probably the #include
itself (maybe) because Serial
and memcpy
aren't yet defined/declared? I tried including the the Arduino.h
library, but that didn't help. Actually, it did help the boolean problem but only in the case of putting everything in the .h
file (as I discuss further below), it doesn't help the above example.
我可以看到错误 2 和错误 3 是相关的,但是我如何在范围内获得这些?我意识到问题的那部分可能是#include
它本身(可能),因为Serial
和memcpy
尚未定义/声明?我尝试包括Arduino.h
图书馆,但这没有帮助。实际上,它确实有助于解决布尔问题,但仅在将所有内容都放入.h
文件的情况下(正如我在下面进一步讨论的那样),它对上面的示例没有帮助。
If I put the three files together and have everything in the main sketch (.ino
) file, it works as it should. But the idea here is that I want to break out some code and make my sketch more readable.
如果我将这三个文件放在一起并将所有内容都放在主草图 ( .ino
) 文件中,它就可以正常工作。但这里的想法是我想分解一些代码并使我的草图更具可读性。
The closest I got to a solution was found here: http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/where, after running my own tests, I determined that if I put EVERYTHING in a .h
file, it works!
我在这里找到了最接近解决方案的方法:http: //liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/在运行我自己的测试后,我确定如果我把所有东西都放在一个.h
文件中,它有效!
For example, leaving the main sketch file unchanged, if I delete a.cpp
and create just a.h
(as follows) it works!
例如,保持主草图文件不变,如果我删除a.cpp
并创建a.h
(如下)它就可以了!
#ifndef H_A
#define H_A
boolean isNegative(int x){
Serial.println("I can't print this from inside my INCLUDE FILE");
if (x<0) return true;
return false;
}
int anotherOdity(){
char ch[5];
memcpy(ch,"1",1); //doesn't work, memcpy not declared
}
#endif // H_A
This fixes the boolean problem (well.... I still need Arduino.h
or char foo;
), and it fixes the scope issues.
这解决了布尔问题(嗯......我仍然需要Arduino.h
or char foo;
),并且解决了范围问题。
But it just feels wrong.
但就是感觉不对。
This isn't about creating a library of standard functions I could use across various sketches, it's all about breaking my code into smaller (readable) chunks, and keeping them all together in the project folder. I want to do this in the most correct way possible, it just seems to be I'm limited by the IDE. I'm sure I have a suitable understanding of how to put a header and associated .cpp
file together (I hope that I have't got that part wrong).
这不是关于创建一个我可以在各种草图中使用的标准函数库,而是关于将我的代码分解成更小的(可读)块,并将它们一起保存在项目文件夹中。我想以最正确的方式做到这一点,这似乎是我受到 IDE 的限制。我确信我对如何将标题和关联.cpp
文件放在一起有适当的理解(我希望我没有弄错那部分)。
I am totally self taught with everything C/C++ and have only really got into programming micros very recently.
我完全自学了 C/C++ 的所有知识,并且最近才真正进入微编程领域。
I have researched this through the depths of google and am just continually coming up short.
我已经通过谷歌的深度研究了这一点,但我一直在做空。
Without resorting to hacks
and keeping it simple for folk like me, how can I best put together the above examples so that the Arduino IDE/gcc will compile it?
hacks
对于像我这样的人来说,如果不诉诸并保持简单,我怎样才能最好地将上面的例子放在一起,以便 Arduino IDE/gcc 编译它?
edit: I thought I would include just SOME of the tabs I have open here to show that I really have done some research on this!
编辑:我想我会只包括我在这里打开的一些标签,以表明我真的对此做了一些研究!
http://arduino.cc/en/Reference/Include
http://arduino.cc/en/Reference/Include
http://arduino.cc/en/Hacking/LibraryTutorial
http://arduino.cc/en/Hacking/LibraryTutorial
http://forum.arduino.cc/index.php/topic,124904.msg938861.html#msg938861
http://forum.arduino.cc/index.php/topic,124904.msg938861.html#msg938861
http://forum.arduino.cc/index.php?topic=84412.0(this is where I found the char foo;
solution)
http://forum.arduino.cc/index.php?topic=84412.0(这是我找到char foo;
解决方案的地方)
http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/
http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/
回答by user2711915
The reason it doesn't work is that you need to include something in your a.h or a.cpp files.
它不起作用的原因是您需要在 ah 或 a.cpp 文件中包含某些内容。
Try this in your a.h file and then everything should work.
在您的 ah 文件中尝试此操作,然后一切正常。
#ifndef H_A
#define H_A
#include <Arduino.h> //needed for Serial.println
#include <string.h> //needed for memcpy
...
The reason for this is that you can think of the compiler separately compiling each cpp file. A #include is in fact just an automated copy paste. When the compiler is coming to compile a.cpp, it doesn't know that Serial.println() exists, because it wasn't defined in a.h, which is the only other text that appears in a.cpp. The reason it works when you put it all in the header is that in your main cpp file you have included Arduino.h before the a.h include, so once those #includes have been copy pasted in its as if you just wrote the code there in the first place.
这样做的原因是你可以认为编译器单独编译每个cpp文件。#include 实际上只是一个自动复制粘贴。当编译器来编译a.cpp 时,它不知道Serial.println() 存在,因为它没有在ah 中定义,这是a.cpp 中唯一出现的其他文本。当您将所有内容都放在标题中时它起作用的原因是,在您的主 cpp 文件中,您在 ah 包含之前包含了 Arduino.h,因此一旦将这些 #includes 复制粘贴到其中,就好像您刚刚在其中编写了代码一样第一名。
You can just write all your code in headers, but it isn't advisable for various reasons including efficiency at compile time (but as an arduino program can only be 32k, I don't think compile times are going to get too long!)
您可以在头文件中编写所有代码,但出于各种原因,包括编译时的效率,这是不可取的(但由于 arduino 程序只能是 32k,我认为编译时间不会太长!)