簡體   English   中英

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

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

一,問題:

主草圖文件:

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
}

以上,因為,不編譯,這些是我得到的錯誤:

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;修復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並創建只是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

這解決了布爾問題(好......我仍然需要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/

包括.cpp文件

將所有庫保存在Arduino sketch目錄中

C ++標題和CPP包括

它不起作用的原因是你需要在ah或a.cpp文件中包含一些東西。

在你的ah文件中嘗試這個,然后一切都應該工作。

#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()是否存在,因為它沒有在ah中定義,這是a.cpp中出現的唯一其他文本。 將它全部放入標題時它起作用的原因是在你的主cpp文件中你已經在ah include之前包含了Arduino.h,所以一旦那些#includes被復制粘貼在它的中就好像你只是在那里編寫代碼一樣第一名。

您可以在頭文件中編寫所有代碼,但由於各種原因(包括編譯時的效率)不可取(但由於arduino程序只能為32k,我認為編譯時間不會太長!)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM