简体   繁体   中英

c makefile with shell commands and variables

This is from a bash script I used to build a program:

dateString=$(date +%Y/%m/%d\ %H:%M:%S)
revision=(`svn info | grep Revision | tr -d [:alpha:]':'`)
echo "#define VERSION_DATE \"$dateString\""     >  version.h
echo "#define VERSION_REVISION \"$revision\""   >> version.h

I changed from using the build.sh to a makefile:

version.h:
    dateString=$$(date +%Y/%m/%d\ %H:%M:%S)
    revision=(`svn info | grep Revision | tr -d [:alpha:]':'`)
    echo "#define VERSION_DATE \"$dateString\""    >  version.h.tmp
    echo "#define VERSION_REVISION \"$revision\""  >> version.h.tmp
    mv version.h.tmp version.h

But the version.h file ends up thusly:

#define VERSION_DATE "\ateString"
#define VERSION_REVISION "\evision"

I cant seem to get the shell variables properly. I think its because they end up being Makefile vars. If anyone know how to do it, I wouldnt mind knowing how. Many thanks.

Remember that every command is run in its own shell, so dateString and revision will be unset in third and fourth command.

So you use semicolons and backslashes at each line's end to make it one command. Also you need to use $$ to refer to shell's $.

Or don't use intermediate variables, then you won't need it to be one command. Something like this:

version.h:
    echo \#define VERSION_DATE \"$$(date +%Y/%m/%d\ %H:%M:%S)\" >  version.h.tmp
    echo \#define VERSION_REVISION \"$$(svn info | grep Revision | tr -d [:alpha:]:)\" >> version.h.tmp
    mv version.h.tmp version.h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM