Noob question: How to print serial from other file?

Global variables are not automatically shared between files when using .cpp as they would be when using .ino. You need to have extern delcarations that declare the existance of variable (in name and type, but not value) to other code files.

In your case, in printer.cpp, the easiest would be to add

#include <Adafruit_Thermal.h>
// defined in main.cpp
extern Adafruit_Thermal printer;

at the top of the printer.cpp file.

The stylistically more correct way would be to create a new header file (e.g., printer.h) with an include guard and the above inner content, that can then be included by printer.cpp, instead of directly putting it in printer.cpp.

This is explained in more detail in multiple other topics, e.g.

1 Like