2016-01-24 314 views
-1

我在編譯我的arduino草圖時遇到了問題。Arduino IDE編譯器錯誤多重定義

我得到這些錯誤:

C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp/bit_array.cpp:6: multiple definition of `Bit_Array::Bit_Array(unsigned long)' 
C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp\bit_array.cpp.o:C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp/bit_array.cpp:6: first defined here 
C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp\Master_Controller_modbus_RTU.cpp.o: In function `Bit_Array::~Bit_Array()': 
C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp/bit_array.cpp:18: multiple definition of `Bit_Array::~Bit_Array()' 
C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp\bit_array.cpp.o:C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp/bit_array.cpp:18: first defined here 
C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp\Master_Controller_modbus_RTU.cpp.o: In function `Bit_Array::~Bit_Array()': 
C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp/bit_array.cpp:18: multiple definition of `Bit_Array::~Bit_Array()' 
C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp\bit_array.cpp.o:C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp/bit_array.cpp:18: first defined here 
C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp\Master_Controller_modbus_RTU.cpp.o: In function `Bit_Array::Set_Value_MSB(unsigned char, unsigned long, unsigned char)': 
C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp/bit_array.cpp:26: multiple definition of `Bit_Array::Set_Value_MSB(unsigned char, unsigned long, unsigned char)' 
C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp\bit_array.cpp.o:C:\Users\FRANCE~1\AppData\Local\Temp\build8652217817452966608.tmp/bit_array.cpp:26: first defined here 
collect2.exe: error: ld returned 1 exit status 
Errore durante la compilazione 

這意味着我已經定義了一個以上的時間我的構造函數,析構函數和方法。 我有點困惑,因爲我只定義了它一次,正如你所看到的。

bit_array.h

#ifndef bit_array 
#define bit_array 

// includo il supporto per le funzioni base dell'arduino 
#if defined(ARDUINO) && ARDUINO >= 100 
    #include "Arduino.h" 
#else 
    #include "WProgram.h" 
    #include <pins_arduino.h> 
#endif 


#include<cstdint> // libreria per supporto tipo uint8_t in C++ standard (arduino lo conosce, ma voglio fare la libreria cross-plattform) 

class Bit_Array 
    { 
    private: 
     unsigned long N_Elementi;     // Numero di bit della memoria effettivamente utilizzati (es 15 bit occupano 2byte, ma solo i primi 15bit hanno significato). 
     uint8_t* Array;       // array di uint8_t. Ogni uint8_t verrà interpretato come una sequenza di 8 bool 
     unsigned long Dimensione;     // Dimensione (in byte) della memoria puntata da Array. Deve essere allocata una quantità di memoria pari a upper_bound(N_elementi/8) 

    public: 
     Bit_Array(const unsigned long Number_of_Elements); 
     ~Bit_Array(); 

     // Getters 
     uint8_t Get_Value_MSB(const unsigned long Inizio, const uint8_t Lunghezza) const; 
     uint8_t Get_Value_LSB(const unsigned long Inizio, const uint8_t Lunghezza) const;   // Implementazione futura 

     // Setters 
     void Set_Value_MSB(const uint8_t B, const unsigned long Inizio, const uint8_t Lunghezza); 
     void Set_Value_LSB(const uint8_t B, const unsigned long Inizio, const uint8_t Lunghezza); // Implementazione futura 
     void Print() const; 
    }; 

#endif 

bit_array.cpp

#include "bit_array.h" 

Bit_Array::Bit_Array(const unsigned long Number_of_Elements) 
    { 
     if(Number_of_Elements <= 0) 
      {Array=NULL;}  // non alloco la memoria 
     else 
      { 
      N_Elementi = Number_of_Elements; 
      Dimensione = (unsigned long) (((double)Number_of_Elements/8.0) + 0.5); // alloco una quantità di memoria sufficiente a memoriazzare i Number_of_Elements. La memoria viene allocata con granularità di byte, quindi devo allocare Number_of_Elements/8 byte e poi arrotondare per eccesso 

      } 
    } 

Bit_Array::~Bit_Array() 
    {if(Array!=NULL) 
     {delete Array; 
     Array=NULL;} 
    } 


void Bit_Array::Set_Value_MSB(const uint8_t B, const unsigned long Inizio, const uint8_t Lunghezza) 
    { 

    } 

我只

#include "bit_array.cpp" 

相同的薄g與所有其他方法一起發生。

使用DevC++ IDE,我可以成功編譯代碼,所以我認爲這個問題與Arduino IDE有關。

非常感謝你對你有所幫助,祝您有一個愉快的一天

+2

_'#include「bit_array.cpp」_ _不包含源文件,這是公然錯誤!包含用於獲取聲明的頭文件並鏈接包含定義的所有'.cpp'文件。 –

+0

#πάνταῥεῖ謝謝你的回覆。 就像你建議我的,主要的,我現在包括.h而不是.cpp,它的工作! 我有一個問題...自3年以來,我總是在主要包括.cpp,並始終工作(使用gcc編譯器)。我在大學學到了。我真的不明白你的「公然錯誤」......我認爲我可以單獨編譯所有的cpp,而不是鏈接.o文件,或者將所有代碼包含在main中並編譯它(就像將所有代碼複製到一個文件中一樣,編譯它)。如果錯了,我會再次研究編譯和鏈接過程。 謝謝! – user3450036

回答

1

我會建議你看看this article,爲了更好地理解include指令。

C++ programs are built in a two stage process. First, each source file is compiled on its own. The compiler generates intermediate files for each compiled source file. These intermediate files are often called object files -- but they are not to be confused with objects in your code. Once all the files have been individually compiled, it then links all the object files together, which generates the final binary (the program).

Even though MyClass is declared in your program, it is not declared in main.cpp, and therefore when you compile main.cpp you get that error.

This is where header files come in. Header files allow you to make the interface visible to other .cpp files

確實,您需要包含頭文件,而不是源文件。希望這可以幫助。