首先,问题是:
主要草图文件:
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
第一个问题是boolean类型,看起来像Arduino环境中的一些名称混乱,但这通常是由主文件中的char foo;
修复的。在某些情况下,它是正确的。但是在.cpp
文件中使用该类型会生成此错误。
我可以看到错误2和错误3是相关的,但是如何在作用域中获得这些错误呢?我意识到问题的一部分可能是#include
本身(可能),因为Serial
和memcpy
还没有定义/声明?我尝试包含Arduino.h
库,但没有帮助。实际上,它确实帮助了布尔问题,但只有在将所有内容都放在.h
文件中的情况下(正如我在下面进一步讨论的那样),它对上面的示例没有帮助。
如果我将这三个文件放在一起,并将所有内容都放在主草图(.ino
)文件中,它就会正常工作。但这里的想法是,我想突破一些代码,使我的草图更具可读性。
我得到的最接近的解决方案是在这里找到的:http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/,在运行我自己的测试之后,我确定如果我把所有东西都放在一个.h
文件中,它就能工作!
例如,保持主草图文件不变,如果我删除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++,直到最近才真正开始对micros编程。
我已经通过谷歌的深度研究了这一点,只是不断地发现不足之处。
对于像我这样的人来说,如果不求助于hacks
并保持它的简单性,我如何最好地将上述示例组合在一起,以便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/
发布于 2014-01-29 00:15:49
它不能工作的原因是你需要在你的a.h或a.cpp文件中包含一些东西。
在你的.h文件中尝试一下,然后一切都会正常工作。
#ifndef H_A
#define H_A
#include <Arduino.h> //needed for Serial.println
#include <string.h> //needed for memcpy
...
这样做的原因是,您可以认为编译器分别编译每个cpp文件。事实上,#include只是一个自动复制粘贴。当编译器编译a.cpp时,它不知道Serial.println()的存在,因为它不是在a.h中定义的,而A.H是a.cpp中唯一出现的其他文本。当你把它全部放在头文件中时,它起作用的原因是在你的主cpp文件中,你已经在a.h include之前包含了Arduino.h,所以一旦这些#includes被复制粘贴到它里面,就像你一开始就在那里写代码一样。
您可以将所有代码都写在头文件中,但出于各种原因(包括编译时的效率),这是不可取的(但由于arduino程序只能有32k大小,我认为编译时间不会太长!)
https://stackoverflow.com/questions/21409042
复制相似问题