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

查看:702
本文介绍了正确的方法,包括的.cpp和.h文件在一个Arduino草图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一,问题:

主要草图文件:

 字符富; //需要清理一些其他的问题
#包括LT&;&Arduino.h GT; //试图在绝望中,没有帮助
#包括A.H无效设置(){
  Serial.begin(9600);
  Serial.println(\\ NTEST开始);
  对于(INT NUM = -1; NUM< 1; NUM ++){
    Serial.print(NUM);
    如果(isNegative(NUM)){
      Serial.println(为负);
    }其他{
      Serial.println(不是消极」);
    }
  }
}空隙环(){}

// A.H

 的#ifndef H_A
#定义H_A布尔isNegative(INT X); //错误#1
INT anotherOdity();#ENDIF // H_A

// a.cpp

 的#includeA.HINT isNegative(INT X){
  Serial.println(我不能从里面打印此我的包含文件); //错误#2
  如果(X小于0)返回true;
  返回false;
}INT anotherOdity(){
  炭CH [5];
  的memcpy(CH,1,1); //不工作,不MEMCPY声明//错误#3
}

以上,为的是,不编译,这些都是我得到的错误:

1:

 在文件从a.cpp包括:
A.H:4:错误:布尔没有指定类型
a.cpp:在函数'诠释isNegative(INT):
a.cpp:3:错误:串行并没有在这一范围内声明
a.cpp:在函数'诠释anotherOdity():
a.cpp:11:错误:'的memcpy'没有在这个范围内声明

第一个问题是布尔类型,似乎一些名字改编的Arduino的环境确实受苦,但一般是由字符富固定; 主文件。并且在某些情况下,它是。但是使用在的.cpp 文件类型生成该错误。

我可以看到错误2和3是相关的,但我怎么范围得到这些?我意识到了问题的一部分可能是的#include 本身(也许),因为串口的memcpy 还没有定义/声明?我想包括在 Arduino.h 库,但没有帮助。事实上,它确实帮助布尔问题,但只有在把一切在 .H 文件的情况下(如我讨论下文),它并不能帮助上面的例子

如果我把三个文件一起,在主草图的一切( .ino )的文件,它的工作,因为它应该。但这里的想法是,我想打破一些code,让我的素描更具可读性。

我得到了一个解决方案最近发现这里:<一href=\"http://liudr.word$p$pss.com/2011/02/16/using-tabs-in-arduino-ide/\">http://liudr.word$p$pss.com/2011/02/16/using-tabs-in-arduino-ide/在那里,我跑自己的测试后,我决定,如果我把一个 .H 文件的一切,它的作品!

例如,离开主素描文件不变,如果我删除 a.cpp 和刚创建(如下图)它的作品!

 的#ifndef H_A
#定义H_A布尔isNegative(INT X){
  Serial.println(我不能从里面打印此我的包含文件);
  如果(X小于0)返回true;
  返回false;
}INT anotherOdity(){
  炭CH [5];
  的memcpy(CH,1,1); //不工作,不MEMCPY声明
}#ENDIF // H_A

这修复了布尔问题(以及....我还需要 Arduino.h 字符富; ),同时,修复范围的问题。

但它只是感觉不对。

这是不是创造的标准功能,我可以在各种草图使用一个库,它是所有关于打破我的code为更小的(可读)块,并在项目文件夹中保存它们放在一起。我想这样做,以最正确的方式,它只是似乎是我的IDE限制。我敢肯定,我对如何把一个头以及相关的的.cpp 文件一起(我希望我have't了这部分错误的)一个合适的理解。

我完全自学的一切C / C ++和只真正进入编程万分之一最近。

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

没有求助于破解,并保持它的简单像我这样的民俗,我如何才能最好地放在一起上面的例子,这样的Arduino的IDE / GCC编译会<? / p>

编辑:我想我只是包括一些我在这里开的标签显示,我真的做了一些研究对这个

http://arduino.cc/en/Reference/Include

<一个href=\"http://arduino.cc/en/Hacking/LibraryTutorial\">http://arduino.cc/en/Hacking/LibraryTutorial

<一个href=\"http://forum.arduino.cc/index.php/topic,124904.msg938861.html#msg938861\">http://forum.arduino.cc/index.php/topic,124904.msg938861.html#msg938861

<一个href=\"http://forum.arduino.cc/index.php?topic=84412.0\">http://forum.arduino.cc/index.php?topic=84412.0 (这是我找到的字符富; 解决方案)

<一个href=\"http://liudr.word$p$pss.com/2011/02/16/using-tabs-in-arduino-ide/\">http://liudr.word$p$pss.com/2011/02/16/using-tabs-in-arduino-ide/

包括.cpp文件

<一个href=\"http://stackoverflow.com/questions/4705790/keeping-all-libraries-in-the-arduino-sketch-directory?rq=1\">Keeping在Arduino的草图目录所有的库

C ++头和CPP包括


解决方案

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

在A.H文件

试试这个,然后一切都应该工作。

 的#ifndef H_A
#定义H_A#包括LT&;&Arduino.h GT; //需要Serial.println
#包括LT&;&string.h中GT; //需要的memcpy...

这样做的原因是,你能想到的编译器编译分开每个CPP文件。一个是的#include其实只是一个自动化的复制粘贴。当编译器来编译a.cpp,它不知道Serial.println()存在,因为它没有在A.H,这是出现在a.cpp唯一的其他文字定义。当你把它所有在头工作原理的原因是,在你的主CPP文件中已包含Arduino.h前有啊,所以一旦这些#包括一直副本以其粘贴为,如果你只是写了code可能会摆在首位。

您可以只写在头文件中的所有code,但它是不可取的各种原因,包括在编译时的效率(但作为一个Arduino程序只能是32K,我不认为编译时间会获得太久了!)

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

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

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

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.

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.

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!

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

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

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

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

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?

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

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

解决方案

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

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

...

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.

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天全站免登陆