Search code examples
ccompiler-errorsesp32arduino-idearduino-esp32

Compilation error: unknown type name 'HardwareSerial'


I need to write a code to send data via UART. I wrote a test code on Arduino IDE in a .ino file and it worked perfectly. Now I trying to shift the code to a .c file. My code is as follows:

#include "Arduino.h"

HardwareSerial obj(0);

It gives me the following error:

 error: unknown type name 'HardwareSerial'
    4 | HardwareSerial obj(0);

I am running the code in a .c file in Arduino IDE. I also tried using the HardwareSerial library but its inclusion gave another error.

Can you please point out how I can make this work?


Solution

  • The problem was that Arduino.h (which contains HardwareSerial) is a C++ library. So when I tried to include it in a C file, it gave an error.

    Changing the file extension from .c to .cpp fixed the issue.