在 Arduino 草图中包含 .cpp 和 .h 文件的正确方法

2024-01-03

一、问题:

主要草图文件:

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 and memcpy尚未定义/声明?我尝试包括Arduino.h图书馆,但这没有帮助。实际上,它确实有助于解决布尔问题,但仅限于将所有内容都放入.h文件(正如我在下面进一步讨论的那样),它对上面的示例没有帮助。

如果我将三个文件放在一起并将所有内容放在主草图中(.ino) 文件,它就可以正常工作。但这里的想法是我想分解一些代码并使我的草图更具可读性。

我最接近的解决方案是在这里找到的:http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/ 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 or char foo;),它解决了范围问题。

但就是感觉不对。

这并不是要创建一个可以在各种草图中使用的标准函数库,而是要将我的代码分解成更小的(可读)块,并将它们全部保存在项目文件夹中。我想以最正确的方式做到这一点,这似乎是我受到 IDE 的限制。我确信我对如何放置标题和关联的内容有适当的理解.cpp一起归档(我希望我没有弄错那部分)。

我完全自学了 C/C++ 的所有知识,直到最近才真正开始接触微编程。

我已经通过谷歌深入研究了这一点,但我只是不断地发现不足。

无需求助于hacks对于像我这样的人来说保持简单,我怎样才能最好地将上述示例组合在一起,以便 Arduino IDE/gcc 能够编译它?

编辑:我想我会只包含我在这里打开的一些选项卡,以表明我确实对此做了一些研究!

http://arduino.cc/en/Reference/Ininclude 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 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/

包括 .cpp 文件 https://stackoverflow.com/questions/19547091/including-cpp-files

将所有库保存在 Arduino sketch 目录中 https://stackoverflow.com/questions/4705790/keeping-all-libraries-in-the-arduino-sketch-directory?rq=1

C++ 头文件和 CPP 包括 https://stackoverflow.com/questions/8893421/c-header-and-cpp-includes?lq=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.h是a.cpp中出现的唯一其他文本。当你把它全部放在头文件中时它起作用的原因是,在你的主cpp文件中,你在a.h包含之前包含了Arduino.h,所以一旦这些#includes被复制粘贴到其中,就好像你刚刚在其中编写了代码一样第一名。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Arduino 草图中包含 .cpp 和 .h 文件的正确方法 的相关文章

随机推荐