Specifying the name of the compiled ".hex" file from within the sketch.

Is there a pre-processor or compiler command that can be used inside a sketch to specify the name of the .hex file generated by the compiler? I'm using the "Export Compiled Binary" command in the IDE to generate a .hex file that will be uploaded later with avrdude and a programming module.

Wouldn't it be simpler to let the IDE produce a hex file with its own choice of name and then make a copy of that file with whatever name you wish?

...R

The sketch contains many blocks of code that are conditionally compiled based on the value of a constant which is set to a value of 1 thru 8 depending on which version of the code I want to build. If the constant is set to "1", I would like the .hex file to have a "1" as part of the filename. Every time I release new code to the factory, I have to set the value of the constant and compile the code 8 times. It would be convenient if I could automatically put the version number in the name of the hex file. Of course I can rename the file after it's compiled, but I'm trying to automate the build process as much as possible. Other commercial compilers that I use can do this, I was just hoping the avr-gcc compiler could also.

It would be nice wouldn't it, if it exists i will use it as well.

slandry:
Every time I release new code to the factory, I have to set the value of the constant and compile the code 8 times. It would be convenient if I could automatically put the version number in the name of the hex file.

I don't use the Arduino IDE for editing my programs. I use the Geany editor for all my programming. I have written this short Python program to compile and upload my code using the Arduino IDE command line. I use the Python program with Geany but it also works stand-alone.

It seems to me it would be a simple matter to write a similar Python program to automate the compiling of all your versions and copy the hex files to any names you choose.

...R

Hi Robin2,

didn't know of Geany until you mentioned it here. Do you happen to know notepad++ ?

If you compare notepad++ with geany what are the advantages of geany over np++ ?
Does geany have an integrated serial monitor?
inside geany are all functions assignable to userdefined hotkeys?
does it have auto-reformatting?

How many keystrokes does it need to compile and upload code?

OK downlaoded and installed it
Is geany modifiable to have a standard open file-dialog. I hate clicking through sub-sub-sub-dirs I use everything to copy path and filename to the clipboard and then paste it into the open-file-dialog

best regards Stefan

slandry:
The sketch contains many blocks of code that are conditionally compiled based on the value of a constant which is set to a value of 1 thru 8 depending on which version of the code I want to build. If the constant is set to "1", I would like the .hex file to have a "1" as part of the filename. Every time I release new code to the factory, I have to set the value of the constant and compile the code 8 times. It would be convenient if I could automatically put the version number in the name of the hex file. Of course I can rename the file after it's compiled, but I'm trying to automate the build process as much as possible. Other commercial compilers that I use can do this, I was just hoping the avr-gcc compiler could also.

If you want to automate it, you should not be using the IDE's GUI. Use a script and the command line interface. You can use build properties (either via the --pref option of the Arduino IDE's CLI or the --build-properties option of Arduino CLI) to set a global preprocessor and also the output filename. I would recommend Arduino CLI, since it was specifically designed for this sort of application:
https://arduino.github.io/arduino-cli/0.13/installation/

Here's an example Bash script to get you started. Of course, the same could be done with a Windows batch file, whatever the powershell scripts are called, Python, etc...

#!/bin/bash

sketch_path="$HOME/Arduino/MySketch"
output_directory="/tmp/MySketchArtifacts"
output_name_base="MySketch"
board_fqbn="arduino:avr:uno"

for variant_number in {1..8}; do
  arduino-cli compile \
    --fqbn "$board_fqbn" \
    --build-properties build.project_name=${output_name_base}${variant_number} \
    --build-properties compiler.cpp.extra_flags=-DVARIANT_NUMBER=$variant_number \
    --output-dir "$output_directory" \
    "$sketch_path"
done

This is compiling the sketch 8 times, incrementing the value of a global macro named VARIANT_NUMBER each time, then saving the .hex and other build artifacts to the folder /tmp/MySketchArtifacts with the files names according to MySketch with the variant number appended. Of course, you can customize all of that to your needs.

$ ./foo.sh > /dev/null && ls -1 /tmp/MySketchArtifacts
MySketch1.eep
MySketch1.elf
MySketch1.hex
MySketch2.eep
MySketch2.elf
MySketch2.hex
MySketch3.eep
MySketch3.elf
MySketch3.hex
MySketch4.eep
MySketch4.elf
MySketch4.hex
MySketch5.eep
MySketch5.elf
MySketch5.hex
MySketch6.eep
MySketch6.elf
MySketch6.hex
MySketch7.eep
MySketch7.elf
MySketch7.hex
MySketch8.eep
MySketch8.elf
MySketch8.hex

The compiler.cpp.extra_flags build property is intended to allow the user to customize the compilation commands with arbitrary flags (in this case it's a -D flag to set the global macro). This will work perfectly with any official Arduino boards platform as well as most 3rd party boards platforms. However, each platform author is free to configure their platforms as they wish, so support for this property is not guaranteed. Some platforms don't implement it and others actually use it for their own internal purposes, even though it's intended to be free for the user. So you may want to check to see what the situation is with the platform you're using.

You can find information on that topic as well as a proposal I made for improving the situation here:

StefanL38:
If you compare notepad++ with geany what are the advantages of geany over np++ ?

I love notepad++, but I had to stop using it because it's Windows-only and I'm regularly using all operating systems these days. There is a semi-clone for Linux called "notepadqq", but it has significant differences from Notepad++, so really doesn't serve my purposes.

Geany can be used on Windows, Linux, and macOS. I spent some time using it, and even though I don't like it as much as Notepad++, it's pretty nice.

However, the standard has become VS Code, which is also cross-platform. Geany didn't win that race. Even though I don't like VS Code very much, I am resigned to it inevitably being the editor I'm going to end up using, so I might as well go straight to it from Notepad++ rather than wasting time becoming proficient with Geany.

StefanL38:
didn't know of Geany until you mentioned it here. Do you happen to know notepad++ ?

I use Linux so I don't know notepad++

I'm sure I don't use a fraction of the capabilities of Geany. I like things simple. The only investigation I made of the more advanced features was to create a menu item to call my Python upload program.

My use of hot-keys is limited to ctrl-c, ctrl-v, ctrl-s.

I'm starting to learn Blender and the first tutorial I watched leads me to think it has dozens of special keys and that will be quite enough for my little brain :slight_smile:

...R

Arduino CLI is definitely the way to go. Thanks for all the great input. Now to get it done!