the original file is like
OkTest.ino (856 位元組)
#include "arduino.h"
class MyClass {
public:
MyClass();
~MyClass();
void setVariable(int);
static int MyVariable;
};
int __attribute__((used)) MyClass::MyVariable __asm__("MyVariable");
MyClass::MyClass(){
MyVariable = 0;
};
MyClass::~MyClass(){
};
void MyClass::setVariable(int input){
register uint8_t tmp;
__asm__ __volatile__(
"mov %0, %A1\n\t" \
"sts MyVariable, %0 \n\t" \
"mov %0, %B1\n\t" \
"sts MyVariable+1, %0\n\t" \
:
:"r" (tmp), "r" (input)
:
);
};
MyClass check;
void setup() {
Serial.begin(9600);
Serial.print("check before setVariable = "); Serial.println(check.MyVariable);
check.setVariable(1000);
Serial.print("check variable MyClass.MyVariable = "); Serial.println(check.MyVariable);
}
void loop() {
}
It works fine...
but when I separate them into individually file like this
ErrorTest.ino (283 位元組)
#include "MyClass.h"
void setup() {
Serial.begin(9600);
Serial.print("check before setVariable = "); Serial.println(check.MyVariable);
check.setVariable(1000);
Serial.print("check variable MyClass.MyVariable = "); Serial.println(check.MyVariable);}
void loop() {
}
MyClass.cpp (458 位元組)
#include "MyClass.h"
int __attribute__((used)) MyClass::MyVariable __asm__("MyVariable");
MyClass::MyClass(){
MyVariable = 0;
};
MyClass::~MyClass(){
};
void MyClass::setVariable(int input){
register uint8_t tmp;
__asm__ __volatile__(
"mov %0, %A1\n\t" \
"sts MyVariable, %0 \n\t" \
"mov %0, %B1\n\t" \
"sts MyVariable+1, %0\n\t" \
:
:"r" (tmp), "r" (input)
:
);
};
MyClass check;
MyClass.h (237 位元組)
#ifndef MyClass_H
#define MyClass_H
#include "arduino.h"
class MyClass {
public:
MyClass();
~MyClass();
void setVariable(int);
static int MyVariable;
};
extern MyClass check;
#endif
then I got the error message
Arduino: 1.8.13 (Windows 10), Board: "Arduino Nano, ATmega328P (Old Bootloader)"
C:\Users\Tracker\AppData\Local\Temp\cckoPtOv.ltrans0.ltrans.o: In function setup': D:\Working\ErrorTest/ErrorTest.ino:5: undefined reference to
MyClass::MyVariable'
D:\Working\ErrorTest/ErrorTest.ino:5: undefined reference to `MyClass::MyVariable'
D:\Working\ErrorTest/ErrorTest.ino:7: undefined reference to `MyClass::MyVariable'
D:\Working\ErrorTest/ErrorTest.ino:7: undefined reference to `MyClass::MyVariable'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Nano.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.