Total failure to get very basic code to compile using Serial on Leonardo

Windows 10
Arduino 1.8.12
Board: "Arduino Leonardo"

I'm having stupid problems trying to use Serial on a Leonardo.

I'm now trying to port a sketch from a NANO to a LEONARDO (too complex to post) and struggling to get it to compile when I try to put the external serial interface into a separate file.

here's the essence of what I'm seeing (I've seen references to both error scenarios in other forum posts but no real solution - they've just gone away)

If I verify this it compiles with no errors

// test.ino

//#include "tty.h"

void setup() {
#if defined(_TTY_H_)
  tty_init();
#else
  Serial.begin(115200);
#endif
}

void loop() {
}

However if I add these two files and un-comment the #include "tty.h"

// tty.h
#ifndef _TTY_H
#define _TTY_H_

extern void tty_init(void);

#endif
// tty.c
//#include "HardwareSerial.h"

#include "tty.h"

void tty_init(void) {
  Serial.begin(115200);
}

I get this error

Compiling sketch...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-gcc" -c -g -Os -Wall -Wextra -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10812 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "C:\\Users\\peter\\AppData\\Local\\Temp\\arduino_build_334670\\sketch\\tty.c" -o "C:\\Users\\peter\\AppData\\Local\\Temp\\arduino_build_334670\\sketch\\tty.c.o"
C:\Users\peter\AppData\Local\Temp\arduino_build_334670\sketch\tty.c: In function 'tty_init':

tty.c:7:3: error: 'Serial' undeclared (first use in this function)

   Serial.begin(115200);

   ^~~~~~

C:\Users\peter\AppData\Local\Temp\arduino_build_334670\sketch\tty.c:7:3: note: each undeclared identifier is reported only once for each function it appears in

exit status 1
'Serial' undeclared (first use in this function)

So I add the #include "HardwareSerial.h" in tty.c and now I get

Compiling sketch...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-gcc" -c -g -Os -Wall -Wextra -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10812 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "C:\\Users\\peter\\AppData\\Local\\Temp\\arduino_build_334670\\sketch\\tty.c" -o "C:\\Users\\peter\\AppData\\Local\\Temp\\arduino_build_334670\\sketch\\tty.c.o"
In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:27:0,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:26,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29,

                 from C:\Users\peter\AppData\Local\Temp\arduino_build_334670\sketch\tty.c:2:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Printable.h:25:1: error: unknown type name 'class'; did you mean 'labs'?

 class Print;

 ^~~~~

 labs

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Printable.h:33:1: error: unknown type name 'class'; did you mean 'labs'?

 class Printable

 ^~~~~

 labs

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Printable.h:34:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token

 {

 ^

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:26:0,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29,

                 from C:\Users\peter\AppData\Local\Temp\arduino_build_334670\sketch\tty.c:2:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:37:1: error: unknown type name 'class'; did you mean 'labs'?

 class Print

 ^~~~~

 labs

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:38:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token

 {

 ^

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29:0,

                 from C:\Users\peter\AppData\Local\Temp\arduino_build_334670\sketch\tty.c:2:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:49:1: error: unknown type name 'class'; did you mean 'labs'?

 class Stream : public Print

 ^~~~~

 labs

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:49:14: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token

 class Stream : public Print

              ^

In file included from C:\Users\peter\AppData\Local\Temp\arduino_build_334670\sketch\tty.c:2:0:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:93:1: error: unknown type name 'class'; did you mean 'labs'?

 class HardwareSerial : public Stream

 ^~~~~

 labs

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:93:22: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token

 class HardwareSerial : public Stream

                      ^

C:\Users\peter\AppData\Local\Temp\arduino_build_334670\sketch\tty.c: In function 'tty_init':

tty.c:7:3: error: 'Serial' undeclared (first use in this function)

   Serial.begin(115200);

   ^~~~~~

C:\Users\peter\AppData\Local\Temp\arduino_build_334670\sketch\tty.c:7:3: note: each undeclared identifier is reported only once for each function it appears in

exit status 1
'Serial' undeclared (first use in this function)

Anybody tell me what I'm missing here?

Thanks

OK, while waiting for help I've continued to explore.

And by looking closely at all the commands issued to compile the sketch i found the problem.

My existing sketch is pure c apart from the .ino file.
The existing tty code is in tty.c and does not use Serial. For latency reasons It uses the UART interrupts directly.

As I've tried to port the sketch I just created an alternate tty.c file trying to use Serial as a first pass - the latency should be less of a problem on this platform (I hope)

The thing is you have to compile code using Serial as c++

So the solution for my full sketch is to put the code in tty.cpp and then bracket all the global prototypes in tty.h and the actual functions in tty.cpp with

#ifdef __cplusplus
extern "C" {
#endif

...

#ifdef __cplusplus
}
#endif

You seem to be making things complicated for yourself.

Why not just do it the normal Arduino way with something like

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
  Serial.println("Leonardo is working");
}

void loop() {
  
}

...R

@robin2

That's not a constructive answer.

My problem was not function it was compilation and build. I demonstrated that I knew it worked putting Serial.begin() in test.ino

As I said I'm porting a very complex sketch from an atm328 to an atm32u4. The code I posted was merely to demonstrate a simple scenario that demonstrated my problem.

The original sketch uses very little Arduino code. All hardware access (including the host serial interface) is direct register access because of the time critical nature of other parts of the firmware. The code is split over multiple files with distinct division of functionality.

The current system could be built using avr tools, the Arduino GUI is just a convenience.

Using Serial is just a first pass in the port process. It's possible that I may find I have to use lower level (LUFA) code directly because of the

peterp57:
@robin2

That’s not a constructive answer.

I guess I misunderstood “I’m having stupid problems trying to use Serial on a Leonardo.” in your Original Post.

…R