首先,问题:
主要的草图文件:
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
#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
}
上述代码不可直接编译,以下是错误信息:
错误信息如下:
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
第一个问题是布尔类型,似乎受到Arduino环境的一些名称缩写的影响,但这通常可以通过主文件中的
char foo;
来解决。但在某些情况下,使用该类型在.cpp
文件中会生成此错误。我可以看出错误2和3相关,但如何使它们在范围内?我意识到问题的部分原因可能是
#include
本身(也许),因为Serial
和memcpy
尚未定义/声明?我尝试包含Arduino.h
库,但没有帮助。实际上,在将所有内容放入.h
文件中的情况下,它确实有助于布尔问题,但它不适用于上面的示例。如果我将三个文件放在一起,并且将所有内容都放在主sketch(
.ino
)文件中,则它会按预期工作。但这里的想法是我想拆分一些代码并使我的sketch更易读。我找到的最接近解决方案在这里找到:http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/ 在运行了自己的测试后,我确定如果我将所有内容都放在一个
.h
文件中,它可以工作!例如,保留主sketch文件不变,如果我删除
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
这解决了布尔问题(嗯...我仍然需要Arduino.h
或char foo;
),并解决了范围问题。
但感觉不对劲。
这与创建可在各种草图中使用的标准函数库无关,而是将我的代码分成更小(可读)的块,并将它们全部放在项目文件夹中。我希望以最正确的方式做到这一点,但似乎受到IDE的限制。我相信我已经适当地理解了如何组合头文件和相关的.cpp
文件(希望我没有弄错这部分)。
我完全是自学C/C++的,最近才真正开始编程微控制器。
我通过谷歌深入研究了这个问题,但一直找不到解决方案。
没有诉诸于hack
,并为像我这样的人保持简单,我该如何最好地组合上述示例,以便Arduino IDE/gcc可以编译它?
编辑:我想我会包含一些我在这里打开的选项卡,以表明我确实对此进行了一些研究!
http://arduino.cc/en/Reference/Include
http://arduino.cc/en/Hacking/LibraryTutorial
http://forum.arduino.cc/index.php/topic,124904.msg938861.html#msg938861
http://forum.arduino.cc/index.php?topic=84412.0(这是我找到char foo;
解决方案的地方)
http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/