2012-05-07 26 views
0

我在一節中多次調用一個宏。宏檢查目錄是否存在,如果不存在,則創建該目錄。在一節中多次使用宏

我的問題:我得到一個錯誤,因爲我從段內部多次調用這個宏。我怎樣才能解決我的編譯錯誤?

錯誤:「錯誤:標籤‘CreateDirThenInstall:’已經在部分宣稱」

你能告訴我怎麼樣,我可以在一節一次使用這個宏不止?

Section "Install Plugin Files" MainSetup 
    !insertmacro ValidateDir "c:/blah" 
    setOutPath "c:/blah" 
    file "C:/blah/a.txt" 
    file "C:/blah/b.txt" 

    !insertmacro ValidateDir "c:/other" 
    setOutPath "c:/other" 
    file "c:/other/a.txt" 
    file "c:/other/b.txt" 
sectionend 

!macro ValidateDir dir 
    IfFileExists "$dir" ExitMacro CreateDirThenInstall 
    CreateDirThenInstall: # Error here: Error: label "CreateDirThenInstall:" already declared in section 
     createDirectory "${dir}" 
    ExitMacro: 
!macroend 

回答

1

問題出在標籤上,而不是用宏。 您在該部分中使用了兩次完全相同的標籤,這是不可能的。

您可以使宏中的標籤唯一(即使宏插入不止一次)。編譯時間命令${__LINE__}可用於此。你可以寫這樣的東西:

!macro ValidateDir dir 
    !define UniqueId1 ${__LINE__} 
    !define UniqueId2 ${__LINE__} 
    IfFileExists "${dir}" Exit_${UniqueId1} CreateDir_${UniqueId2} 
    CreateDir_${UniqueId2}: 
     createDirectory "${dir}" 
    Exit_${UniqueId1}: 
    !undef UniqueId1 
    !undef UniqueId2 
!macroend 

但在你的情況,我認爲上述是沒有必要的。如果需要,SetOutPath指令會創建目錄。從DOC:

Sets the output path ($OUTDIR) and creates it (recursively if necessary), if it does not exist.

所以,如果你不需要知道關於創建每個目錄(和它的地方寫,例如,在卸載過程中使用它),你不必這樣做的。