【问题标题】:correct way to include .cpp and .h files in an Arduino sketch在 Arduino 草图中包含 .cpp 和 .h 文件的正确方法
【发布时间】:2014-01-28 14:58:14
【问题描述】:

首先,问题:

主草图文件:

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 本身(可能),因为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 的限制。我确定我对如何将标头和关联的.cpp 文件放在一起有适当的理解(我希望我没有弄错那部分)。

我完全是自学 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

【问题讨论】:

    标签: c++ include arduino header-files arduino-ide


    【解决方案1】:

    它不起作用的原因是你需要在你的 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 文件中你在 a.h 包含之前包含了 Arduino.h,所以一旦这些#includes 被复制粘贴在其中,就好像你只是在那里写了代码一样第一名。

    您可以将所有代码都写在头文件中,但出于各种原因,包括编译时的效率,这是不可取的(但由于 arduino 程序只能是 32k,我认为编译时间不会太长长!)

    【讨论】:

    • 我不敢相信这么简单,我已经为此苦苦挣扎了好几天:(我现在遇到了更多错误,但那是在添加其他内容之后,我学习了一些更多。谢谢,现在看来答案很明显。
    • 没问题。开始总是很困难,尤其是 C++ 的一些奇怪的怪癖。去年我买了一个 arduino,用起来真的很有趣。有趣的选择 c++ 作为它的默认语言,但我认为这是由于硬件级别太低以至于你无法在其中安装虚拟机。
    猜你喜欢
    • 2022-11-27
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    相关资源
    最近更新 更多