From the trenches – Arduino compilation and link errors and fixes to those


Error 1 – Loader error of undefined reference
Arduino: 1.6.5 (Windows 8.1), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

myrobot.cpp.o: In function `__static_initialization_and_destruction_0′:
C:\Program Files (x86)\Arduino/myrobot.ino:57: undefined reference to `Michelino::Robot::Robot()’
myrobot.cpp.o: In function `loop’:
C:\Program Files (x86)\Arduino/myrobot.ino:272: undefined reference to `Michelino::Robot::run()’
collect2.exe: error: ld returned 1 exit status
Error compiling.

Solution
The problem is that the cpp file should not have the word class in it and only the header file.

Original code :

namespace test
{
class Ctest
{
public:
Ctest() {};
void a() {};
};
};

test.h
#ifndef rocketbot_remote_control_h
#define rocketbot_remote_control_h
namespace test
{
class Ctest
{
public:
// Ctest();
void a();
};
};
#endif

main

#include “test.h”

using namespace test;
test::Ctest r;
//Ctest r;

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:
r.a();
}

Fixed Code
Test.cpp
#include “test.h”
namespace test
{
//void Ctest::Ctest() {};
void Ctest::a() {};
};

Test.h
#ifndef rocketbot_remote_control_h
#define rocketbot_remote_control_h
namespace test
{
class Ctest
{
public:
// Ctest();
void a();
};
};
#endif

Main
#include “test.h”

using namespace test;
test::Ctest r;
//Ctest r;

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:
r.a();
}

Error 2 – Function not found

Arduino: 1.6.5 (Windows 8.1), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

In file included from robot_vehicle_with_bth.h:61:0,
from myrobot.ino:55:
rocketbot_remote_control.h:11: error: no matching function for call to ‘SoftwareSerial::SoftwareSerial()’
SoftwareSerial BTSerial;
^
rocketbot_remote_control.h:11:16: note: candidates are:
In file included from robot_vehicle_with_bth.h:34:0,
from myrobot.ino:55:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial/SoftwareSerial.h:90:3: note: SoftwareSerial::SoftwareSerial(uint8_t, uint8_t, bool)
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial/SoftwareSerial.h:90:3: note: candidate expects 3 arguments, 0 provided
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial/SoftwareSerial.h:47:7: note: SoftwareSerial::SoftwareSerial(const SoftwareSerial&)
class SoftwareSerial : public Stream
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial/SoftwareSerial.h:47:7: note: candidate expects 1 argument, 0 provided
no matching function for call to ‘SoftwareSerial::SoftwareSerial()’
Solution
Add extern keyword in header file
extern SoftwareSerial BTSerial;

Error 3 — redefinition of ‘class Michelino::RemoteControl’
Arduino: 1.6.5 (Windows 8.1), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

In file included from myrobot.ino:56:0:
rocketbot_remote_control.h:14: error: redefinition of ‘class Michelino::RemoteControl’
class RemoteControl : public RemoteControlDriver
^
In file included from robot_vehicle_with_bth.h:61:0,
from myrobot.ino:55:
rocketbot_remote_control.h:14: error: previous definition of ‘class Michelino::RemoteControl’
class RemoteControl : public RemoteControlDriver
^
redefinition of ‘class Michelino::RemoteControl’

Solution :
Fix to add the following since header file gets included multiple times
#ifndef rocketbot_remote_control_h
#define rocketbot_remote_control_h
….
#endif

Error 4
x.cpp file
robot_vehicle_with_bth.cpp:77: error: ‘Robot’ has not been declared
void Robot::Robot()

Solution
Solution is to include x.h file
#include “x.h”

Stay tuned more fun ones coming!

Advertisements

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: