Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remember at run time, relevant compiling data

Programming

remember at run time, relevant compiling data

by  guggach  Posted    (Edited  )
#!/bin/ksh
# Q: witch version is this ?
# Q: when compiled, which compiler ?
# Q: on witch system ?
# Q: where are the src ?

# this is a shell example, sure it's better (so do i)
# to implement all the stuff dynamically in Makefile

# change if needed
export MYVERSION=0.0.1

# on which system was this code compiled
# purge blancs, so only one quoting level is needed
export MYSYSTEM=`uname -a|sed -e 's/ /_/g'`

# use the full date output, purge blancs
export MYDATE=`date|sed -e 's/ /_/g'`

# remember compiler version
export MYCOMPILER=abc_2.2.3.4.5_123

export MYAUTHOR=guggach@yahoo.com

# to faster locate the src-code
export MYSRCNAME=`pwd`

# feel free to define other relevant data
# source this file and
# append to cc lines all values, see below
# start make


cc -o sample version.c -DMYSRCNAME=\"$MYSRCNAME/version.c\" -DMYCOMPILER=\"$MYCOMPILER\" -DMYVERSION=\"$MYVERSION\" -DMYSYSTEM=\"$MYSYSTEM\" -DMYAUTHOR=\"$MYAUTHOR\" -DMYDATE=\"$MYDATE\"

# execute sample, you should get

sample --version
sample: is /export/home/c-src/version.c
sample: src-version 0.0.1
sample: compiled with abc_2.2.3.4.5_123
sample: on SunOS_uno_5.7_Generic_sun4u_sparc_SUNW,Ultra-1
sample: at Wed_Jun_16_09:04:27_MET_DST_2004
sample: by guggach@yahoo.com


NOTA: the macros
__TIME__ is not known on all systems
__DATE__ is too short, prefer the full date output
__FILE__ is the src-name at compiling time not the
exec-name at running time (could be a symlink)





#### version.c ----------------------
#include <stdio.h>
#define GETVERSION "--version"
int main(int argc, char **argv)
{
extern char *basename(char *);
char *myname = basename(*argv++);

if(*argv && !strcmp(*argv,GETVERSION))
exit(printversion(myname));

/* your code here */

exit(0);
}



/* make an object with this, so you can reuse it */

int printversion(char *name)
{
#ifdef MYSRCNAME
printf("%s: is %s\n",name,MYSRCNAME);
#endif
#ifdef MYVERSION
printf("%s: src-version %s\n",name,MYVERSION);
#endif
#ifdef MYCOMPILER
printf("%s: compiled with %s\n",name,MYCOMPILER);
#endif
#ifdef MYSYSTEM
printf("%s: on %s\n",name,MYSYSTEM);
#endif
#ifdef MYDATE
printf("%s: at %s\n",name,MYDATE);
#endif
#ifdef MYAUTHOR
printf("%s: by %s\n",name,MYAUTHOR);
#endif
return(0);
}
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top