Code::Blocks: SVN 10120
O/S: Ubuntu 12.04 x86_64
I can quite happily create, compile and run a command line program, so all good so far.
Then I decided I wanted to define a variable TIMESTAMP_ISO. So on the Project build options page, Compiler settings, #defines, I enter:
TIMESTAMP_ISO=$(shell date -u '"\"%Y-%m-%dT%H:%M:%SZ\""')
as per a post elsewhere. The problem when compiling:
date No such file or directory
Excuse me? date is in /bin where it belongs. I can run from command line, I can run by going /bin/sh -c date
Seriously at a loss why this is not working. Help?
M
Why do you think the #defines page support this format?
Have you inspected the full build log?
What does shell mean?
The example given in full was:
-DTIMESTAMP_ISO=$(shell date -u '"\"%Y-%m-%dT%H:%M:%SZ\""')
in a makefile. The defines page I understand is the one to use to implement the equivalent preprocessor command.
The build log only gave the text back I posted previously.
The program Environments lists shell as being /bin/sh -c
M
C::B supports backticks to run external commands, something like ...
TIMESTAMP_ISO=`date -u +"%Y-%m-%d %H:%M:%SZ"`
within Compiler settings->#defines should work.
Note: C::B caches external commands and therefore the result will not change within the current session.
In fact there are some time/date related variables that could be a bit more portable.
See here: http://wiki.codeblocks.org/index.php?title=Variable_expansion
Hi all,
Thank you for the replies. The back ticks worked on the #defines. Though under Windows I needed to do a bit of cludge to get the whole item in a string (used a GNU port of date).
So thank you.
Quote from: xxxxviii on April 17, 2015, 02:01:47 AM
Hi all,
Thank you for the replies. The back ticks worked on the #defines. Though under Windows I needed to do a bit of cludge to get the whole item in a string (used a GNU port of date).
So thank you.
Did you look into the link oBFusCATed has posted, specially: http://wiki.codeblocks.org/index.php?title=Variable_expansion#Time_and_date (http://wiki.codeblocks.org/index.php?title=Variable_expansion#Time_and_date) ?
Jens,
Yes, I did. What I did in the end was this:
On the defines page
Linux version
BUILD_DATE=`date -u +"%F"`
BUILD_TIME=`date -u +"%T"`
Windows version
BUILD_DATE=`F:\bin\gnudate.exe -u +"%F"`
BUILD_TIME=`F:\bin\gnudate.exe -u +"%T"`
in my main.cpp
#define xstr(s) #s
#define str(s) xstr(s)
#define BUILD_TIMESTAMP_ISO str(BUILD_DATE) "T" str(BUILD_TIME) "Z"
then later in the module as the report header:
fprintf(stdout, "%s (build %s %s)\n", MODULE_DESC, MODULE_VERSION, BUILD_TIMESTAMP_ISO);
which at execution prints something like:
SPDIAGNOSTIC (build 9.0.1 2015-04-17T05:20:03Z)
which is exactly what I was after.
M
P.S. reproducing from memory so please forgive any syntax mistake
So we need a variable that returns the time only...
Not necessarily. The macros __DATE__ returns the compile date and __TIME__ the compile time. My issue (and I stress my issue) was I do not like the format returned. Turning it into an ISO format date YYYY-mmm-ddThh:mm:ssZ or YYYY-mm-ddThh:mm:ss+nnnn (for local time) is frustrating with the way the preprocessor works. It can be accomplished with around a page or so of code or shorter if you write a function that formats and returns a string.
The solution I have outlined works in three lines of source code and two define statements (could 'cheat' and use __TIME__ as this is the same as my second line).
The other frustration is producing a Windows version as Windows date is no where near as useful as the Linux date. Thankfully there was a GNU port with MinGW core utils (I could have written my own of course but time constrained).
It was just a matter of getting it in the right part of Code::Blocks.
M