2014-01-28 122 views
9

首先,問題:正確的方法,包括的.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 

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

上述原樣不符合ILE,這些都是我得到的錯誤:

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本身(可能),因爲Serialmemcpy尚未定義/聲明?我嘗試了包括Arduino.h庫,但這沒有幫助。實際上,它確實有助於布爾問題,但只有在將所有內容都放入.h文件的情況下(我將在下面進一步討論),它並不能幫助上述示例。

如果我把這三個文件放在一起,並且在主草圖(.ino)文件中包含所有內容,它就會像它應該那樣工作。但這裏的想法是,我想打破一些代碼,使我的素描更具可讀性。

我在這裏找到了最接近我的解決方案: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.hchar foo;),同時,修復範圍的問題。

但它只是感覺不對。

這不是關於創建一個我可以在各種草圖中使用的標準函數庫,而是將我的代碼拆分爲更小的(可讀的)塊,並將它們全部保存在項目文件夾中。我想以最正確的方式做到這一點,它似乎是我受IDE限制的。我確定我對如何把頭文件和相關的文件放在一起(我希望我沒有弄錯那部分)有一個合適的理解。

我完全自學了一切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/

Including .cpp files

Keeping all libraries in the Arduino sketch directory

C++ Header and CPP includes

回答

12

它不工作的原因是,你需要在你的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.cpp中唯一出現的其他文本。把它放在標題中的原因是,在你的主要cpp文件中,你已經包含了Arduino.h,所以一旦這些#includes被複制粘貼在其中,就好像你只是在那裏寫了代碼一樣第一個地方。

你可以直接在頭文件中寫所有的代碼,但是由於各種原因,包括編譯時的效率,這是不可取的(但作爲一個arduino程序只能是32k,我不認爲編譯時間會得到太長了!)

+1

我不敢相信這很簡單,我一直在這方面掙扎幾天:(我現在越來越多的錯誤,但這是在添加其他的東西后,我的主題學習更多感謝你,現在看起來這樣一個明顯的答案。 – Madivad

+0

沒問題,開始時總是很困難,特別是有一些奇怪的C++怪癖。去年我得到了一個arduino,使用起來非常有趣。對於它的默認語言,但我認爲這是由硬件如此低層次的事實引起的,你只是在那裏也不適合虛擬機。 – user2711915