Why preprocessor behaves differently in #include d

2019-02-20 16:10发布

问题:

Was trying to understand the difference between a syntax that would include another script file and a source file in Inno Setup script while using macros to search and find files.

I have tried to use FindFolder function from Find a directory using wildcard in Inno Setup:

#define FindFolder(Path) \
    Local[0] = FindFirst(Path, faDirectory), \
    Local[0] ? AddBackslash(ExtractFileDir(Path)) + FindGetFileName(Local[0]) : Path

Like this:

#include "{#FindFolder('..\..\..\packages\ScriptPreRequisites*')}\DotNetDependencies.iss"

回答1:

Within # you are in the "realm" of Inno Setup preprocessor.

There are two ways to enter preprocessor realm.

  • Full line syntax:

    #directive args
    
  • And inline syntax:

    {#directive args}
    

    The inline syntax is almost exclusively used for emit directive - {#emit <expression>}. And for this reason there's a shorthand format, with the emit omitted: {#<expression>}.

The inline syntax is useful when you want to use preprocessor expression outside of preprocessor realm. Like in normal Inno Setup sections (or in Pascal Script code):

[Files]
Source: "{#FindFolder("..\packages\PackagesA*")}\*.*"; DestDir: "{app}"; \
    flags: recursesubdirs  

Though in #include preprocessor directive, you are already in the preprocessor realm. And there, the {#xxx} syntax is invalid (it might even have a different [valid] meaning theoretically, but actually curly brackets have no use in preprocessor).

The syntax of #include directive is:

#include <expression>

The preprocessor uses C-like expression syntax. So your expression in this case is:

FindFolder('..\..\..\packages\ScriptPreRequisites*') + '\DotNetDependencies.iss'