Hello all, I have got a large code with a huge number of #if defined() and #ifdef blocks. Most of these code blocks are unused and has no meaning to me. Is there any tool to preprocess these conditional compilation blocks to get rid of unused ones and shorten the code? Thanks, Petr
The code between the #ifdef/ifndef and #endif is treated conditionally by the pre-processor. If it is not needed, as in the test conditions will never be met, simply delete it. If you think you may need it just ignore it. If the conditions are not met during pre-processing, the code will not be compiled/added to your sketch.
Turn on verbose compile messages in Preferences. Click on the Verify button to compile your sketch. In the output, look for the line "Compiling sketch...". The (very long) line after that is the compile command. For example:
"/Users/john/Library/Arduino15/packages/arduino/tools/avr-gcc/4.9.2-atmel3.5.4-arduino2/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-I/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.21/cores/arduino" "-I/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.21/variants/standard" "-I/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.21/libraries/SoftwareSerial/src" "/var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/arduino_build_916251/sketch/FingerprintReader.ino.cpp" -o "/var/folders/cs/p6yz0z1m8xj9lf0059b_lzw00000gn/T/arduino_build_916251/sketch/FingerprintReader.ino.cpp.o"
Copy that line and paste it into a command window. Add " -E" at the end and run the command. The output file (the last path, right after the "-o" option flag) will now contain the pre-processed source. Your sketch will be down at the end, after all the code from all of the included files.
John, That probably is not what ok1fig was asking for. A fully prepossessed source file is typically not all that useful for continued development as it does a lot more pre-processing than just removing specific undesired conditional sections and will also have all the macro substitutions done as well.