2014-01-28 120 views
9

首先,问题:正确的方法,包括的.cpp和.h文件在一个Arduino草图

主要素描文件:

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(){} 

//啊

#ifndef H_A 
#define H_A 

boolean isNegative(int x);     // Err#1 
int anotherOdity(); 

#endif // H_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 
} 

上述原样不符合ILE,这些都是我得到的错误:

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本身(可能),因为Serialmemcpy尚未定义/声明?我尝试了包括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.hchar foo;),同时,修复范围的问题。

但它只是感觉不对。

这不是关于创建一个我可以在各种草图中使用的标准函数库,而是将我的代码拆分为更小的(可读的)块,并将它们全部保存在项目文件夹中。我想以最正确的方式做到这一点,它似乎是我受IDE限制的。我确定我对如何把头文件和相关的文件放在一起(我希望我没有弄错那部分)有一个合适的理解。

我完全自学了一切C/C++,并且最近才真正进入编程微软。

我已经通过谷歌的深度研究了这一点,我只是不断地做出来。

没有求助于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/

Including .cpp files

Keeping all libraries in the Arduino sketch directory

C++ Header and CPP includes

回答

12

它不工作的原因是,你需要在你的A.H或a.cpp文件什么的。

在你的a.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.cpp中唯一出现的其他文本。把它放在标题中的原因是,在你的主要cpp文件中,你已经包含了Arduino.h,所以一旦这些#includes被复制粘贴在其中,就好像你只是在那里写了代码一样第一个地方。

你可以直接在头文件中写所有的代码,但是由于各种原因,包括编译时的效率,这是不可取的(但作为一个arduino程序只能是32k,我不认为编译时间会得到太长了!)

+1

我不敢相信这很简单,我一直在这方面挣扎几天:(我现在越来越多的错误,但这是在添加其他的东西后,我的主题学习更多感谢你,现在看起来这样一个明显的答案。 – Madivad

+0

没问题,开始时总是很困难,特别是有一些奇怪的C++怪癖。去年我得到了一个arduino,使用起来非常有趣。对于它的默认语言,但我认为这是由硬件如此低层次的事实引起的,你只是在那里也不适合虚拟机。 – user2711915