Serial.print undeclared

Hello,
I’m getting a compiler error when I try to use Serial.print in a sketch file (not the main sketch file.) I’ve included Arduino.h and tried including WProgram.h as mentioned in some older posts (that file doesn’t exist in Arduino 1.6.8.) Is there another header file I should include?

#include <Arduino.h>

void test()
{
  Serial.print("This is a test.");
}

Arduino: 1.6.8 (Windows 7), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

sketch\test.c: In function ‘test’:

test.c:5: error: ‘Serial’ undeclared (first use in this function)

Serial.print(“This is a test.”);

^

Have you got a Serial.begin?

(Always best to show full sketch not just a snip)

sketch\test.c

Serial.xxx is a C++ object; try renaming our file to test.cpp instead of test.c

1 Like

Yes, I have Serial.begin in my main sketch file

void setup() {
  // put your setup code here, to run once:
  Serial.begin(19200);

}

void loop() {
  // put your main code here, to run repeatedly:

}

So that's the grand total of my sketch: main.ino and test.c.

I renamed test.c to test.cpp and get the same error when compiling. Any more suggestions?

Westfw,
That did work. Apparently when I renamed test.c to test.cpp the IDE was still using test.c for the subsequent compile. Exiting and re-launching the IDE did the trick!

Thanks!

1 Like