Undefined Reference to 'Serial'

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is driving me crazy, because I think it is something simple, just can't figure it out.  I have been using Studio 7 for a while and really like it.  This is my first use of a sketch, and I am having issues linking header files, but it may have nothing to do with the sketch, but how I am approaching things

 

So here is the deal, I have an arduino sketch brought in (Sketch.cpp) in one project and the Arduino core in another project both in one solution.  The Arduiono core compiles great, but I am getting the undefined reference on the other project.   I have the sketch, all header files and cpp in the same folder; Sketch.cpp, SerialCommand.h, SerialCommand.cpp, HardwareSerial.h, HardwareSerial.cpp (with others)

 

Header in Sketch.cpp looks like this:

/*Beginning of Auto generated code by Atmel studio */
#include <Arduino.h>

/*End of auto generated code by Atmel studio */
#include <SerialCommand.h>
#include <AccelStepper.h>
#include "configuration.h"
#include <HardwareSerial.h>

 

in Sketch.cpp, I am using Serial as such:

  Serial.print("ABSOLUTE ROTATION ");
            Serial.print(pos);
            Serial.print(" => ");
            Serial.println(StepsToRotation(stepper1.currentPosition()));

 

I can right mouse click, and go to the implementation, which takes me to HardwareSerial.h, at this line:

#if defined(UBRRH) || defined(UBRR0H)
  extern HardwareSerial Serial;
  #define HAVE_HWSERIAL0
#endif

 

Why does the right menu know where to jump, and the auto fill identify the methods of Serial, but when I compile, it is undefined?

Attached output.

Help!

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you need to add HardwareSerial0.cpp  to your project.

Bob. Engineer and trainee Rocket Scientist.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

aneastonfarmer wrote:
and go to the implementation,
That is the declaration but not the definition. Most good IDEs (Eclipse) for example lets you toggle between declaration and definition.

 

Bottom line; .h is not the code - it's the documentation of the code that is actually provided in .cpp

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson - understand, implementation should have been declaration.  Sketch.cpp has theHardwareSerial.h file, h file declares the variable Serial, and the variable, when typed into Sketch.cpp when coding, Intellisense shows all the methods coded in HardwareSerial.cpp.  So Studio interface seems to like things, don't understand why Sketch.CPP throws undefined reference to Serial. Any ideas?

 

Bob, why did you come to the conclusion that HardwareSerial0.cpp is needed?

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Serial is one of the internal libraries to the Arduino IDE.   You don't have to do #include for its library file in the Arduino sketch.  I believe that the HardwareSerial library is a sub-class of Serial. In that case, you don't need to do an #include HardwareSerial.h because Arduino IDE does that for you.   By the way, Arduino sketches always have .ino extensions (except really old sketch code, which uses .pde extension).  

 

Are you using the Studio 6/7/8 to compile the Arduino code?  Does this code compile with the current Arduino IDE version?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

aneastonfarmer wrote:
Bob, why did you come to the conclusion that HardwareSerial0.cpp is needed?
Have you looked at core/arduino? Serial.h (etc) are the headers that instantiate objects for which the implementation is in hardwareserial.cpp

 

HardwareSerial.h = declaration

Hardwareserial[,0..3].cpp = definition

 

That's why I said you need BOTH the declaration and the definition.

Last Edited: Wed. Feb 8, 2017 - 10:42 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

aneastonfarmer wrote:

Bob, why did you come to the conclusion that HardwareSerial0.cpp is needed?

 

1) I understand how C++ works

2) I looked at the code https://github.com/arduino/Ardui...

 

With those two steps, nearly all programming problems can be solved. Understanding the difference between declaration and definition is fundamental to any language I would say. http://www.cprogramming.com/decl...

 

Beware of what the editor tells you - it can be misleading. Believe the compiler!

Bob. Engineer and trainee Rocket Scientist.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta - perfect - that gave me the clue I needed.  Compiles in Arduino IDE, so knew it was Studio.  I rebuilt the project, found that Serial was declared in both in both projects (duplicate header files, and Atmel Studio throws an undefined error if things are ambiguous.  Removed the include and all compiles great in Studio too.  Studio compiles the Arduino project first, then the custom code project.

 

Loaded to the AVR, all is great!

 

Some notes:

When you import an .ino, Studio makes a CPP called Sketch.cpp with the .ino code in it

Studio looks across projects in a solution, and if you have the same extern variable declared in them, get an undefined error.

Once I removed the include statement, Studio showed two paths when you attempt to go to the implementation.  That identified Serial declared in two locations, which, once removed, solved it!

 

Bob, I know the difference between declaration and definition, I had said implementation.  in Studio, Alt+G will take you to the implementation, or where it is declared.  Once you get into the declaration in the header file, Alt+G will take you to the implementation or the definition of the class in the cpp.  The class is in HardwareSerial.cpp.  So no need for HardwareSerial0.cpp.

 

I appreciate everyone's help.  Moving code from one dev environment to another can be tricky!