简体   繁体   English

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

[英]correct way to include .cpp and .h files in an Arduino sketch

First, the problem: 一,问题:

main sketch file: 主草图文件:

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

// ah //啊

#ifndef H_A
#define H_A

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

#endif // H_A

// a.cpp // 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
}

The above, as is, doesn't compile and these are the errors I get: 以上,因为,不编译,这些是我得到的错误:

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

The first problem is the boolean type, seems to suffer from some name mangling that the Arduino environment does, but that is generally fixed by the char foo; 第一个问题是布尔类型,似乎遭受了Arduino环境所做的一些名称修改,但这通常由char foo;修复char foo; in the main file. 在主文件中。 And in certain situations, it is. 在某些情况下,确实如此。 But to use that type in the .cpp file generates this error. 但是在.cpp文件中使用该类型会生成此错误。

I can see that Errors 2 and 3 are related, but how do I get these in scope? 我可以看到错误2和3是相关的,但我如何在范围内获得这些? I realise that part of the problem is probably the #include itself (maybe) because Serial and memcpy aren't yet defined/declared? 我意识到问题的一部分可能是#include本身(也许)因为Serialmemcpy尚未定义/声明? I tried including the the Arduino.h library, but that didn't help. 我尝试包含Arduino.h库,但这没有帮助。 Actually, it did help the boolean problem but only in the case of putting everything in the .h file (as I discuss further below), it doesn't help the above example. 实际上,它确实有助于布尔问题,但仅在将所有内容放入.h文件的情况下(我将在下面进一步讨论),它对上述示例没有帮助。

If I put the three files together and have everything in the main sketch ( .ino ) file, it works as it should. 如果我将三个文件放在一起并将所有内容放在主草图( .ino )文件中,它就可以正常工作。 But the idea here is that I want to break out some code and make my sketch more readable. 但这里的想法是我想要打破一些代码并使我的草图更具可读性。

The closest I got to a solution was found here: http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/ where, after running my own tests, I determined that if I put EVERYTHING in a .h file, it works! 我找到了最接近解决方案的地方: http//liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/在运行我自己的测试后,我确定如果我把一切都放在.h文件中,它可以工作!

For example, leaving the main sketch file unchanged, if I delete a.cpp and create just ah (as follows) it works! 例如,保持主草图文件不变,如果我删除a.cpp并创建只是ah (如下)它的工作原理!

#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

This fixes the boolean problem (well.... I still need Arduino.h or char foo; ), and it fixes the scope issues. 这解决了布尔问题(好......我仍然需要Arduino.hchar foo; ),它修复了范围问题。

But it just feels wrong. 但它只是感觉不对。

This isn't about creating a library of standard functions I could use across various sketches, it's all about breaking my code into smaller (readable) chunks, and keeping them all together in the project folder. 这不是关于创建我可以在各种草图中使用的标准函数库,而是将我的代码分解为更小(可读)的块,并将它们保存在项目文件夹中。 I want to do this in the most correct way possible, it just seems to be I'm limited by the IDE. 我想以最正确的方式做到这一点,它似乎只是受到IDE的限制。 I'm sure I have a suitable understanding of how to put a header and associated .cpp file together (I hope that I have't got that part wrong). 我确信我对如何将标题和相关的.cpp文件放在一起有一个合适的理解(我希望我没有错误)。

I am totally self taught with everything C/C++ and have only really got into programming micros very recently. 我完全自学了C / C ++的所有内容,并且最近才进入编程微程序。

I have researched this through the depths of google and am just continually coming up short. 我已经通过谷歌的深度研究了这一点,我只是不断出现。

Without resorting to hacks and keeping it simple for folk like me, how can I best put together the above examples so that the Arduino IDE/gcc will compile it? 如果不依赖hacks并让像我这样的民众保持简单,我怎样才能最好地整合上面的例子,以便Arduino IDE / gcc能够编译它?

edit: I thought I would include just SOME of the tabs I have open here to show that I really have done some research on this! 编辑:我想我会包括我在这里打开的一些标签,以表明我对此做了一些研究!

http://arduino.cc/en/Reference/Include 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 (this is where I found the char foo; solution) 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/

Including .cpp files 包括.cpp文件

Keeping all libraries in the Arduino sketch directory 将所有库保存在Arduino sketch目录中

C++ Header and CPP includes C ++标题和CPP包括

The reason it doesn't work is that you need to include something in your ah or a.cpp files. 它不起作用的原因是你需要在ah或a.cpp文件中包含一些东西。

Try this in your ah file and then everything should work. 在你的ah文件中尝试这个,然后一切都应该工作。

#ifndef H_A
#define H_A

#include <Arduino.h> //needed for Serial.println
#include <string.h> //needed for memcpy

...

The reason for this is that you can think of the compiler separately compiling each cpp file. 原因是你可以想到编译器分别编译每个cpp文件。 A #include is in fact just an automated copy paste. #include实际上只是一个自动复制粘贴。 When the compiler is coming to compile a.cpp, it doesn't know that Serial.println() exists, because it wasn't defined in ah, which is the only other text that appears in a.cpp. 当编译器来编译a.cpp时,它不知道Serial.println()是否存在,因为它没有在ah中定义,这是a.cpp中出现的唯一其他文本。 The reason it works when you put it all in the header is that in your main cpp file you have included Arduino.h before the ah include, so once those #includes have been copy pasted in its as if you just wrote the code there in the first place. 将它全部放入标题时它起作用的原因是在你的主cpp文件中你已经在ah include之前包含了Arduino.h,所以一旦那些#includes被复制粘贴在它的中就好像你只是在那里编写代码一样第一名。

You can just write all your code in headers, but it isn't advisable for various reasons including efficiency at compile time (but as an arduino program can only be 32k, I don't think compile times are going to get too long!) 您可以在头文件中编写所有代码,但由于各种原因(包括编译时的效率)不可取(但由于arduino程序只能为32k,我认为编译时间不会太长!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM