Cool little C preprocessor macro trick

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Most of you C gurus will already know this but I thought I would share it anyway...

I usually like to include and display (either on LCD or Terminal) the compile date of my project just so I know where I left off. I used to manually change the date but I found a cool new (to me anyway) way to do it. Now if I forget to change the date, my compiler will automatically do it for me :)

Compiler: ICCAVR v6.31a (Imagecraft)

Old way:

 const char strDate[]={"June 28/2005"};   

New way:

 const char strDate[]={__DATE__};    

Gives the Date format "Jun 28 2005" automatically

Also there is the macro __TIME__ which gives the time the project was compiled...cool eh!

 const char strTime[]={__TIME__}; 

Gives the Time format "19:30:00" and will change every time you recompile your code

Now you can diplay the exact time and date you compiled your code so you will always remeber where you left off.

I guess remebering what you were doing in the first place is another macro.... :shock:

Oc.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Very cool. But I bet it would really throw the rom checksummer for a loop! Hey! Something changed! Configuration Management alert!

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does anybody know, how to insert the compiling date / time with GCC? - I think it is not (so easily) possible!? Or am I wrong?

Michael

In the beginning was the Word, and the Word was with God, and the Word was God.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Its an ANSI macro isnt it? Try it to see if it works?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

skotti wrote:
Does anybody know, how to insert the compiling date / time with GCC?

See the OP. IIRC, it's standard for all C compilers.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And you still have the __LINE__ and __FILE__ macros. And in GCC
you have, if I recall correctly, the __FUNCTION__ macro.
Now guess what they do ;)

Embedded Dreams
One day, knowledge will replace money.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, under GCC something like

const char date_time[] PROGMEM = { "Date:" __DATE__ ", Time:" __TIME__ };

works fine...

Thanks,
Michael

In the beginning was the Word, and the Word was with God, and the Word was God.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
Its an ANSI macro isnt it? Try it to see if it works?

Yes, __DATE__ and __TIME__ (and others) are Predefined macro names called out by the ANSI C standard.

Don