正确的方法包括.cpp和.h文件在Arduino草图 [英] correct way to include .cpp and .h files in an Arduino sketch

查看:3881
本文介绍了正确的方法包括.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

// 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

第一个问题是布尔类型,似乎遭受一些名称的改变,Arduino环境,但是一般由主文件中的 char foo; 修复。在某些情况下,它是。但是在 .cpp 文件中使用该类型会生成此错误。

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; in the main file. And in certain situations, it is. But to use that type in the .cpp file generates this error.

我可以看到错误2和3是相关的,但是如何在范围内获得这些?我意识到问题的一部分可能是 #include 本身(也许),因为 Serial memcpy 尚未定义/声明?我尝试包括 Arduino.h 库,但这没有帮助。实际上,它确实帮助了布尔问题,但只有在把所有的东西放在 .h 文件中(如下面进一步讨论),它不能帮助上面的例子。

I can see that Errors 2 and 3 are related, but how do I get these in scope? I realise that part of the problem is probably the #include itself (maybe) because Serial and memcpy aren't yet defined/declared? I tried including the the Arduino.h library, but that didn't help. 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.

如果我把三个文件放在一起,并且在主草图( .ino )文件中有一切,工作原理。但这里的想法是,我想打破一些代码,使我的素描更可读。

If I put the three files together and have everything in the main sketch (.ino) file, it works as it should. But the idea here is that I want to break out some code and make my sketch more readable.

我得到一个解决方案的最近我在这里找到: http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/ 其中,在运行我自己的测试后,我确定如果我把EVERYTHING在 .h 文件,它的工作原理!

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!

例如,如果我删除 a.cpp ,并创建 ah (如下)它的工作原理!

For example, leaving the main sketch file unchanged, if I delete a.cpp and create just a.h (as follows) it works!

#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

这修复了布尔问题(well ....我还需要 Arduino.h char foo; ),它修复了范围问题。

This fixes the boolean problem (well.... I still need Arduino.h or char foo;), and it fixes the scope issues.

但它只是感觉错了。

这不是关于创建一个我可以在各种草图中使用的标准函数库,它是关于将我的代码分成更小的(可读)块,一起在项目文件夹中。我想以最正确的方式做到这一点,它似乎是我受到IDE的限制。我相信我有一个合适的理解,如何把一个标题和相关的 .cpp 文件在一起(我希望我没有错了部分)。

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. 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).

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

I am totally self taught with everything C/C++ and have only really got into programming micros very recently.

我通过google的深度研究了这一点,我只是不断地出现短。

I have researched this through the depths of google and am just continually coming up short.

无需使用 hacks ,并且像我这样的民间保持简单,我如何将上述示例这样Arduino IDE / gcc会编译它吗?

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?

编辑:我想我会包括我在这里打开的一些标签,以显示我真的做了一些研究这个!

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 /包含

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://forum.arduino.cc/index.php?topic=84412.0 (this is where I found the char foo; solution)

http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/

包括.cpp文件

将所有库保存在Arduino sketch中目录

C ++头和CPP包括

推荐答案

它不起作用的原因是,在您的ah或a.cpp文件中。

The reason it doesn't work is that you need to include something in your a.h or a.cpp files.

在您的ah文件中尝试此操作,然后一切都应该工作。

Try this in your a.h file and then everything should work.

#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之前的ah include,所以一旦这些#includes已复制粘贴在其中,就像你刚刚写的代码在那里第一个地方。

The reason for this is that you can think of the compiler separately compiling each cpp file. A #include is in fact just an automated copy paste. When the compiler is coming to compile a.cpp, it doesn't know that Serial.println() exists, because it wasn't defined in a.h, which is the only other text that appears in 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 a.h include, so once those #includes have been copy pasted in its as if you just wrote the code there in the first place.

你可以在头文件中写所有的代码,但它不建议包括编译时的效率(但作为一个arduino程序可以只有32k,我认为编译时间不会太长!)

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!)

这篇关于正确的方法包括.cpp和.h文件在Arduino草图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆