Adding header and cpp files to Arduino folder

Updated on 27 August 2021
dev board Arduino Zero
features files header cpp src folder
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Code

Download code more-files.ino
#include "src/foo/foo.h"

void setup() {
    SerialUSB.begin(9600);
    while (!SerialUSB) { }

    SerialUSB.println("Start!");
}
void loop() {
    bar();
    delay(2000);
}

Makefile

BOARD?=arduino:samd:arduino_zero_native
PORT := $(shell ls /dev/cu.usbmodem*)

.PHONY: default lint all flash clean

default: lint all flash clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright *.ino

all:
	arduino-cli compile --fqbn $(BOARD) ./

flash:
	arduino-cli upload -p $(PORT) --fqbn $(BOARD)

clean:
	rm -r build

Serial console

Serial output from the firmware.

Adding header and cpp files to Arduino folder serial console

Description

This example shows how to add more files (such as *.h and *.cpp) to a current Arduino project without making it into an Arduino library.

Example folder structure:

.
├── Makefile
├── main.ino
└── src
    └── foo
        ├── foo.cpp
        └── foo.h

References