Referencing a library name/version within itself.


If I create a library called (for example) test.h and test.cpp is there anyway of referencing the file name and file date within the code?

The reason is that I sometimes come back to an arduino project after a week or twos break, plug it into the serial monitor, watch it start to work, and then realise that I have no idea which versions of the libraries are currently in use? What I have done when working with other technologies is to include a reference to the library name and file date in the code that can then be logged to screen (or file) when the project runs - that way when I'm forced to debug I know exactly what files (and versions) are causing the problem.

Most of the time I make a backup before I build, so this isn't normally a problem, but on the odd occasion I forget the backup it would be very helpful if the library name and date were logged.


There are some define variables called FILE and DATE (or suchlike) part of the standard C/C++ stuff, you can put in a Serial.print in the setup. Your library needs to include a function or class-member to return it for the library for the setup to call and print.

The C preprocessor define FILE, DATE and TIME for you at compile time. This is not a perfect solution, but I have been known to do something like this:

char version_ident[] PROGMEM = __DATE__ "," __TIME__;

Then later use version_ident[] in status messages and such.

You can use FILE, DATE and TIME to record when the software was compiled.

If you want to know which version of a given file was used, various version control systems make that available using SCCS IDs and similar but otherwise there is no standard way other than that. You could easily add a hand-coded version number in the relevant files if you wanted, although you'd need to remember to change it when necessary and that is likely to be error-prone.

I often add

define TEST_LIB_VERSION "0.1.04"

to be able to ask the version number,

similar you could ask the name of the sketch

I often add

define TEST_LIB_VERSION "0.1.04"

So do I - but this does rely on me remembering to update it!

You can use FILE, DATE and TIME

This is useful, although ideally I'd like a method of get the data and time of the file.

Thanks for the suggestions guys!