inno-setup

Include once capability in Inno Setup Preprocessor?


I am creating a library of script methods and only want to include them when needed in my installers.

Some of the methods need to use other methods so I was putting the #include "filename.iss" for the needed methods in the file with the method that needs it.

However, if I include two files in an installer script file that also include a common file I get a "Duplicate identifier" error in the second file that includes it.

I've searched for something like #include-once but can't find any results for it.

To reproduce you can just include the same file twice:

#include "AddReplaceLinesInFile.iss"
#include "AddReplaceLinesInFile.iss"

The only way I can see to avoid this is to not put the includes in the files with the methods that need them and just add them in the main installer script.

I'm leaving the includes in the top of the method files that need them but commenting them out - for documentation purposes and to make it easy to copy and paste them into my main installer script. However I'd prefer to use something like an include-once capability.

Does Inno Setup have anything like include-once or a way to test for an already defined method so I could create something similar?

TIA


Solution

  • Use the same trick as is used in C/C++, the include guard:

    In the C and C++ programming languages, an #include guard, sometimes called a macro guard, header guard or file guard, is a particular construct used to avoid the problem of double inclusion when dealing with the include directive.


    Surround the code in the included file with #ifndef UniqueName, #define UniqueName ... #endif:

    #ifndef IncludeIss
    #define IncludeIss
    
    procedure Test;
    begin
    end;
    
    #endif
    

    The UniqueName is typically the same as filename, with punctuation removed (to make it a valid identifier). I.e. for include.iss the name can be IncludeIss.


    See Inno Setup Preprocessor: #ifdef, #ifndef, #ifexist, #ifnexist.