Skip to content

Makefiles should use CXXFLAGS for C++ flags, not CPPFLAGS #4621

Open
@cousteaulecommandant

Description

@cousteaulecommandant

Reference: https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html

Currently, quite a few Arduino makefiles use CPPFLAGS (and other custom macros beginning with CPP) as the C++ counterpart for CFLAGS.
However, "CPP" in this context means "C preprocessor", not "C plus plus". The right name for C++ flags is CXXFLAGS (think of the X as a turned +).
From the Gnu make documentation:

the recipe used to compile a C source file actually says ‘$(CC) -c $(CFLAGS) $(CPPFLAGS)’

The affected files are:

javier@desktop:/tmp/Arduino-master$ grep ^CPP -lIR .
./hardware/arduino/sam/variants/arduino_due_x/build_gcc/gcc.mk
./hardware/arduino/sam/variants/arduino_due_x/build_gcc/libvariant_arduino_due_x.mk
./hardware/arduino/sam/firmwares/atmega16u2/arduino-usbserial/makefile
./hardware/arduino/avr/firmwares/atmegaxxu2/arduino-usbserial/makefile
./hardware/arduino/avr/firmwares/atmegaxxu2/arduino-usbdfu/makefile
./hardware/arduino/avr/bootloaders/caterina/Makefile
./hardware/arduino/avr/bootloaders/caterina-Arduino_Robot/Makefile
./hardware/arduino/avr/bootloaders/caterina-LilyPadUSB/Makefile

This is probably not causing any trouble since it seems that implicit recipes are not being used and these flags are explicitly passed to the respective compiler commands, but it might be misleading to developers and could cause some trouble in a future; I think it would be nice to fix it so that it makes sense.

Additionally, I've noticed some makefiles use LDFLAGS for linked libraries (such as -lm); these should go on LDLIBS instead, since LDFLAGS is (usually) passed BEFORE the source/object files (like most of the flags), but linked libraries must be passed AFTER them.

Alternatively, if all these are just being used as custom variables and you don't care about their "official" meaning, then do not use these special names and use custom ones (for example, cxxflags instead of CXXFLAGS), as recommended in https://www.gnu.org/software/make/manual/html_node/Using-Variables.html for "internal" variables.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: BootloaderThe bootloader is the program used to load the uploaded program into the microcontroller's memoryComponent: CoreRelated to the code for the standard Arduino APIComponent: FirmwareLimited to the contents of the firmwares folder in the core packagesType: Bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions