×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Programming

remember at run time, relevant compiling data by guggach
Posted: 16 Jun 04

#!/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);
}

Back to C FAQ Index
Back to C Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close