Which library/header is 'Serial' declared?

Hi everyone,

I’m getting started writing my Arduino sketches inside of Microsoft Visual Studio and I’ve gone through the documented process of including several Libraries in the Arduino folder structure.

I’ve noticed, though, that even after #include-ing <Arduino.h>, I’m getting the “error: identifier “Serial” is undefined”.

I’ve tried searching the internet and looking through some libraries like Stream.h, but I can’t seem to figure out what I’m missing. I’m sure this is an easy answer, so I apologize if this is the wrong place to ask this.

Thanks!

It's defined in hardwareSerial.h

KenF: It's defined in hardwareSerial.h

that did the trick! thanks

1 Like

nevermind…spoke too soon. Thought HardwareSerial.h did the trick, but I still see the same error after saving my project. I am using Serial.begin() inside of the void setup{} function… is that ok? or do I need to say which namespace Serial is in? here is a snippet of code:

#include <Arduino.h>
#include <HardwareSerial.h>
#include <Servo.h>

#define pinUSSensor1 ‘A0’
#define pinUSSensor2 ‘A1’
#define pinAngle ‘A2’

void setup(){

pinMode(pinUSSensor1, INPUT);
pinMode(pinUSSensor2, INPUT);
pinMode(pinAngle, INPUT);

Serial.begin(9600);
}

Ah but Serial is not a class, it's an incidence of hardwareSerial. It gets defined in hardwareSerial.cpp (which you haven't included) In fact all of the functionality of the hardwareSerial class will be defined there.

By default the IDE will automatically include any CPP files for included libraries. Visual studio isn't going to do that for you. (AFAIK)

In the IDE You'll want to look in the subdirectory hardware\arduino\cores\arduino\

You'll also find the main.cpp there that you may need to include in your "sketch" if you want it to run setup and loop in the same fashion as normal. It may be simpler just to copy the main function into your own cpp file.

It gets defined in hardwareSerial.cpp (which you haven't included)

Actually, the instance is defined in the header file, and one does NOT include cpp files.

The HardwareSerial.h file is included in Arduino.h, so explicitly including it in the sketch is not necessary.

OPs problem has to do with using Visual Studio, so it is not appropriate for the Programming section of the forum.

I'm getting the "error: identifier "Serial" is undefined".

That's a weird error. If I try to compile a sketch that uses Serial in bare gcc without the correct includes, I get something like:

foo.cpp: In function 'void setup()': foo.cpp:24:3: error: 'Serial' was not declared in this scope

If I have the include paths set up but don't link in the libraries, I get:

In function setup': foo.cpp:(.text+0x10): undefined reference toSerial' foo.cpp:(.text+0x12): undefined reference to `Serial'

Could you extract more context from your compile log? In particular, are you seeing compile errors or link errors?