Autotools:$VAR、$(VAR) 和 ${VAR} 以及 ${CC-cc} 与 ${CC} 的差异

Autotools: differences in $VAR, $(VAR) and ${VAR} and ${CC-cc} vs. ${CC}

我看到正在使用 $CFLAGS 和 ${CFLAGS},configure.ac 使用 $CFLAGS,而 acinclude.m4 使用 ${CFLAGS}。所以变量的访问方式不同:$VAR, $(VAR) and/or ${VAR}。

我相信 $(VAR) 和 ${VAR} 是一样的东西,这真的取决于偏好。但是 $VAR 和 $(VAR)/${VAR} 有什么区别,有没有用括号?

还有这段代码:${CC-cc},当我显示变量的内容时,我得到"gcc"。如果我去掉“-cc”,只显示${CC}的内容,我仍然得到"gcc".

那么这里连字符的实际作用是什么?

I see both $CFLAGS and ${CFLAGS} being used, configure.ac uses the $CFLAGS and acinclude.m4 uses the ${CFLAGS}. So variables are accessed differently: $VAR, $(VAR) and/or ${VAR}.

是也不是。

configure.acacinclude.m4 和其他用于构建 configure 脚本的 M4 源包含 shell 代码和 Autoconf 宏的混合,有一点shell 和 M4 都使用 $ 字符来引入变量引用,但您看到的可能主要是 shell 变量引用。对于shell来说,$VAR作为一个完整的句法单元,与${VAR}意义相同;两者都执行“parameter expansion”。大括号可用于将变量名与其上下文分开,例如在 ${VAR}foo 中,以指示变量名是 VAR,而不是 VARfoo。此外,在使用某些特殊参数扩展功能时需要大括号。

I believe $(VAR) and ${VAR} are the same things, and this really depends on preference. But what about $VAR vs. $(VAR)/${VAR}, what's the difference between using brackets or not?

这取决于上下文。 $(VAR) 与 shell 的含义完全不同于 ${VAR} 的含义:它扩展为通过 运行 (文字)命令 VAR 获得的输出(如果有的话) .您不会在 Autoconf 代码中看到它与 ${VAR} 互换使用。但是您可能会在 Makefiles 和 Automake Makefile.am 文件中看到它。这是因为 make 使用 different variable expansion rules 而不是 shell。对它来说,$(VAR)${VAR} 确实意味着同一件事——它们扩展到变量 VAR 的值——但是 $VAR 意味着完全不同的东西(并且可能是错误)。带括号的形式比较传统,但是现在花括号的形式也被广泛使用。

Also with this piece of code: ${CC-cc}, when I display the contents of the variable, I get "gcc". If I take out the "-cc" and just display the contents of ${CC}, I still get "gcc".

So what does the hyphen actually do here?

这是shell参数扩展语法的特点之一(因此它适用于Autoconf代码,但不适用于Automake代码):${CC-cc}扩展为变量[=31=的值],与 ${CC} 相同,前提是 CC 已设置 ,但如果 CC 未设置,则 ${CC-cc} 扩展为文字 cc.