While developing a fairly extensive project I've created a lot of global variables and many of them may now be redundant.
Its making the program difficult to follow.
Is there any easy way to find them so I can delete them?
There are a few old threads about this but pre-date IDE2
Comment them all out and compile the code. Uncomment the ones that cause errors. Once compiled successfully, delete any remaining commented out declarations.
More sophisticated IDEs have the feature you want, like IntelliJ-IDE and PyCharm, but you can't use those with Arduino code, and Arduino IDE is free.
static analyzer command line tools such as cppcheck or clang --analyze could probably report unused variables .
(if you have multiple .ino it's more tricky as they are combined before compilation and so the IDE breaks the cpp variable scope rules and you would need to combine the files yourself for exploration )
If you turn up the level of compiler warnings, maybe there will be warnings about unused declarations. I guess you will get an ocean of other warnings too, so I would deliberately declare something you know isn't used like int iAmNotNeeded;. Find that in the compiler warnings output and you will know the exact format of the warning, enabling you to find others.
Global variables declared as static in one compilation unit (eg .cpp file) are unavailable in other compilation units. Whether or not that's a down side depends on your use case.
@ptillisch editing to rename them all "static" wouldnt be easy as I have rather a lot of different variable types and multiple iso's and .h's.
The lesson I think is to be more systematic in declaring and naming variables, and also to rely less on global declarations.
I need to move some bits of code to different .isos and when I've done that I'll print them out and see where locals could be used instead of globals.
Thanks all for your advice
Using .h and .cpp files in the standard way would go a long way toward solving issues like this. It would also provide your code the modularity that using the "Arduino Way" of multiple .ino files doesn't. See My Post #5 in this Thread for a basic guide to break up large programs into .cpp / .h files.
CTRL-F (find) the variable (without the # sign) will give you a number of how many times a search term is used. If the result is 1, then the variable is not used anywhere else in the sketch.
Put all the actual global definitions in a single file. "globals.c", "globals.ino", whatever.
The linker, as used by the Arduino IDE, does automatically omit unused globals from the final binary, and some boards (ARM boards in particular) do produce a linker map in the build directory. If you're using such a board, or if your code can be compiled for such a board (even if it's not the a actual target), the end of the .map file has a list of "Discarded input sections" that will include unused global variables from your sketch (plus a lot from the Arduino core, alas.)
If I add a line int z, zz, zzz, zzzz; to the Blink example and compile for Arduino Zero, the Blink.ino.map produce will contain:
Discarded input sections
.text 0x0000000000000000 0x0 /Applications/Arduino-1.8.13.app/Contents/Java/portable/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/../lib/gcc/arm-none-eabi/7.2.1/thumb/v6-m/crti.o
.data 0x0000000000000000 0x0 /Applications/Arduino-1.8.13.app/Contents/Java/portable/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/../lib/gcc/arm-none-eabi/7.2.1/thumb/v6-m/crti.o
:
: bunch more discarded segments from the core.
:
var/folders/gv/zn3wcml52jq0vvjnd95j8g6h0000gp/T/arduino_cache_970164/core/core_arduino_samd_arduino_zero_native_2df40d67209c4d62f9ef8cfcc9f8a451.a(hooks.c.o)
/tmp/Arduino1.8.13Build/../../var/folders/gv/zn3wcml52jq0vvjnd95j8g6h0000gp/T/arduino_cache_970164/core/core_arduino_samd_arduino_zero_native_2df40d67209c4d62f9ef8cfcc9f8a451.a(delay.c.o)
z /tmp/Arduino1.8.13Build/sketch/Blink.ino.cpp.o
zz /tmp/Arduino1.8.13Build/sketch/Blink.ino.cpp.o
zzz /tmp/Arduino1.8.13Build/sketch/Blink.ino.cpp.o
zzzz /tmp/Arduino1.8.13Build/sketch/Blink.ino.cpp.o
(the lines thaht contain "/sketch/sketchname.ino.cpp.o" are the ones you're looking for.)