Author Topic: #defines and shell issue  (Read 6479 times)

Offline xxxxviii

  • Single posting newcomer
  • *
  • Posts: 5
#defines and shell issue
« on: April 16, 2015, 06:52:39 am »
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

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: #defines and shell issue
« Reply #1 on: April 16, 2015, 10:42:55 am »
Why do you think the #defines page support this format?
Have you inspected the full build log?
What does shell mean?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline xxxxviii

  • Single posting newcomer
  • *
  • Posts: 5
Re: #defines and shell issue
« Reply #2 on: April 16, 2015, 02:43:23 pm »
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

Offline osdt

  • Multiple posting newcomer
  • *
  • Posts: 63
Re: #defines and shell issue
« Reply #3 on: April 16, 2015, 07:18:44 pm »
C::B supports backticks to run external commands, something like ...

Code
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.
« Last Edit: April 16, 2015, 08:23:42 pm by osdt »

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: #defines and shell issue
« Reply #4 on: April 16, 2015, 09:27:24 pm »
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
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline xxxxviii

  • Single posting newcomer
  • *
  • Posts: 5
Re: #defines and shell issue
« Reply #5 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.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: #defines and shell issue
« Reply #6 on: April 17, 2015, 06:06:48 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 ?

Offline xxxxviii

  • Single posting newcomer
  • *
  • Posts: 5
Re: #defines and shell issue
« Reply #7 on: April 18, 2015, 12:19:54 pm »
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

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: #defines and shell issue
« Reply #8 on: April 18, 2015, 12:34:23 pm »
So we need a variable that returns the time only...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline xxxxviii

  • Single posting newcomer
  • *
  • Posts: 5
Re: #defines and shell issue
« Reply #9 on: April 18, 2015, 01:33:16 pm »
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