编译我的arduino草图时遇到问题。
我收到这些错误:
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"
同样的事情发生在我的所有其他方法上。
使用DevC ++ IDE,我可以成功编译代码,所以我认为这个问题与Arduino IDE有关。
非常感谢你的帮助,祝你今天愉快
答案 0 :(得分:1)
我建议你看一下this article,以便更好地理解include
指令。
C ++程序是在两个阶段的过程中构建的。首先,每个源文件都是自己编译的。编译器为每个编译的源文件生成中间文件。这些中间文件通常称为目标文件 - 但不要将它们与代码中的对象混淆。一旦所有文件都被单独编译,就会将所有目标文件链接在一起,从而生成最终的二进制文件(程序)。
即使你的程序中声明了MyClass,它也没有在main.cpp中声明,因此当你编译main.cpp时会出现错误。
这是头文件的来源。头文件允许您使界面对其他.cpp文件可见
的确,您需要包含头文件,而不是源文件。希望这会有所帮助。