简体   繁体   English

c makefile with shell命令和变量

[英]c makefile with shell commands and variables

This is from a bash script I used to build a program: 这是我用来构建程序的bash脚本:

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: 我从使用build.sh更改为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: 但是version.h文件最终结束:

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

I cant seem to get the shell variables properly. 我似乎无法正确获取shell变量。 I think its because they end up being Makefile vars. 我认为它是因为它们最终成为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. 请记住,每个命令都在自己的shell中运行,因此在第三个和第四个命令中将取消设置dateString和revision。

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 $. 你还需要使用$$来引用shell的$。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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