!define 和 /D 的优先级 - 如何设置可覆盖的默认值?

Precedence of !define and /D - How can I set an overridable default value?

我正在为通常由我的同事安装的系统构建安装程序,但有几件事可能因一台目标计算机而异,包括编译时要安装的文件的位置机器。我以为我可以在源代码中设置一个 "gflag"(使用 !define)并在对 makensis.exe 的调用中覆盖它(使用 /D),但我什至无法获得我的安装程序识别出 /D 标志已通过。

/h 标志后面有更多相关文档:

PS C:\> &"C:\Program Files (x86)\NSIS\makensis.exe" /h
Usage:
  makensis [ option | script.nsi | - ] [...]

Options:
  #...
  /Ddefine[=value] defines the symbol "define" for the script [to value]
  #...

我正在使用这个 NSIS 代码:

!ifndef test
    !define test Foo
!endif

Section
    DetailPrint "${test}"
SectionEnd

我在 PowerShell 中编译安装程序:

&"C:\Program Files (x86)\NSIS\makensis.exe" "C:\path\to\test.nsi" /Dtest=Bar

在输出中,我看到了这个:

Command line defined: "test=Bar"

安装程序已成功创建,但它打印 Foo 而它应该打印 Bar。如果我评论!define test Foo,我在编译时得到一个警告:

1 warning:
  unknown variable/constant "{test}" detected, ignoring (C:\Mach4\Installer\test.nsi:6)

然后打印${test},说明gflag没有值。为什么它没有 Bar 的值?

命令行按顺序解析。在您的情况下,这意味着在解析 /Dtest=Bar 之前解析脚本。试试这个:

"C:\Program Files (x86)\NSIS\makensis.exe" /Dtest=Bar "C:\path\to\test.nsi"