Mixing C and C++ (again): 'Serial' undeclared

I'm returning to the Arduino world after developing a lot of dedicated embedded C code. And I'm immediately stumped on something simple. Here's the error message:

/var/folders/7x/bsz1mn9d5ps8pzs886s71d9m0000gq/T/arduino_build_250184/sketch/taos_test_support.c: In function 'taos_assert':
taos_test_support.c:8:5: error: 'Serial' undeclared (first use in this function)
     Serial.print(func);
     ^

Here's my sketch and supporting C file:

// file: taos_list_test.ino
#include "taos_test_support.h"

void setup() {
  Serial.begin(19200);
  Serial.println("hello...");
  TAOS_ASSERT(1 == 2);
}

void loop() {
}
// file: taos_test_support.h
#ifndef TAOS_TEST_SUPPORT_H
#define TAOS_TEST_SUPPORT_H

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

#define TAOS_ASSERT(expr) taos_assert((expr), __func__, __FILE__, __LINE__)

void taos_assert(const bool expr,
                 const char *const func,
                 const char *const file,
                 const int line);

#ifdef __cplusplus
}
#endif

#endif // #ifndef TAOS_TEST_SUPPORT_H
// file: taos_test_support.c
#include "taos_test_support.h"
// #include <Serial.h> -- see note

void taos_assert(const bool expr,
                 const char *const func,
                 const char *const file,
                 const int line) {
  if (!expr) {
    Serial.print(func);
    Serial.print(" failed:");
    Serial.print(file);
    Serial.print(":");
    Serial.print(line);
    Serial.println();
  }
}

Other info:

  • Arduino v 1.8.8 for MacOS
  • Board = Adafruit ItsyBitsy 32u4

It does not surprise me that Serial is undeclared. But I've not yet figured out how to include. On line 3 of taos_test_support.c, I've tried pretty much every combination I can think of:

#include <Serial.h>
#include <Serial>
#include "Serial.h"
#include <serial.h>
#include <serial>

... but those all result in "Serial: No such file or directory" or similar errors.

What am I missing?

You can use C code in C++, no problem, but you can't use C++ code in C. If you're using the Serial object, you're writing C++, not C. So use the correct file extension: taos_test_support.cpp

@pert:

Thanks.

I renamed taos_test_support.c to taos_test_support.cpp and added #include <Serial.h> at line two of the file. I'm now getting:

taos_test_support.cpp:2:20: error: Serial.h: No such file or directory

So, I'm clearly not understanding something from your answers. Can you elaborate?

There is no file named Serial.h. You can bring in the full Arduino standard core library API (including Serial) like this:

#include <Arduino.h>

If you only want the hardware serial functionality, I believe you can do this:

#include <HardwareSerial.h>

Many thanks. That worked.

You're welcome. I'm glad to hear it's working now. Enjoy!
Per