arduino / arduino/Arduino

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

Open
#4,621 0 comments 2 reactions 0 assignees View on GitHub
Component: Bootloader Component: Core Component: Firmware Type: Bug
Dominant language
Java
Stars
14.6k
Forks
7k
PR merge metrics
No merged PRs in 30d

Description

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](https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html):

> 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.

Contributor guide

Open the contributing guide

Research direction

Inspect the eight affected Makefiles listed in the issue, beginning with the SAM build files and AVR firmware and bootloader Makefiles. Compare the meanings of CPPFLAGS, CXXFLAGS, LDFLAGS, and LDLIBS against the GNU make documentation. Done means the files use standard variable names consistently without changing the intended compiler or linker arguments.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
build-system
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.