簡體   English   中英

編譯錯誤,因為我兩次調用宏

[英]Compile Error because I call a Macro twice

當我兩次(在部分中)調用宏函數時,出現此編譯錯誤:

錯誤:已經在本節中聲明的標簽“ CheckForRMSCustomisationLoop:”

我理解它是因為我兩次定義了一個標簽(跳轉),對嗎?

如何避免這個問題?

我必須將宏轉換為函數還是有更簡單的方法? 因為轉換為函數意味着我無法傳遞參數,所以我必須使用堆棧。

Outfile "RequireAdmin.exe"
RequestExecutionLevel admin  ;Require admin rights on NT6+ (When UAC is turned on)
!include LogicLib.nsh

!macro Test param1 param2

    TestLabel1:
        DetailPrint "TestLabel1"
    TestLabel2:
        DetailPrint "TestLabel2"

!macroend

Section

    !insertmacro Test 1 2
    !insertmacro Test 3 4

  IfErrors 0 +2
    MessageBox MB_ICONEXCLAMATION|MB_OK "Unable to write to the registry" IDOK +1
SectionEnd

Function TestFunct # I MISS MY PARAMS :(

    Pop $R9 # represents param2: but actually having param2 is SO MUCH more descriptive
    Pop $R8 

    TestLabel1:
        DetailPrint "TestLabel1"
    TestLabel2:
        DetailPrint "TestLabel2"

FunctionEnd

/* Usage
    Push 1
    Push 2
    Call TestFunct
    Push 3
    Push 4
    Call TestFunct
*/

沒錯,您要兩次定義標簽TestLabel1TestLabel2

您可以使用以下方法使標簽名稱唯一:

!macro Test param1 param2
    !define ID ${__LINE__}
    TestLabel1_${ID}:
        DetailPrint "TestLabel1"
    TestLabel2_${ID}:
        DetailPrint "TestLabel2"
!macroend 

此處的更多信息:

http://nsis.sourceforge.net/Macro_vs_Function#labels

看來這是一個解決方案,它也很優雅:

!macro Test param1 param2 uid
    TestLabel1_${uid}:
        DetailPrint "TestLabel1"
    TestLabel2_${uid}:
        DetailPrint "TestLabel2"
!macroend 

# Usage:
Section
    !insertmacro Test 1 2 ${__LINE__}
    !insertmacro Test 3 4 ${__LINE__}
SectionEnd
!macro test p1
!define test_ "test${__LINE__}"
IntCmp ${p1} 3 ${test_}double 0
DetailPrint ${p1}
Goto ${test_}end
${test_}double:
IntOp $0 ${p1} * 2
DetailPrint $0
${test_}end:
!undef test_
!macroend

; If the macro is very large and you call it multiple times it might be better to use a function or a macro/function hybrid:
!include util.nsh
!macro testmacroasfunction
Pop $0
IntCmp $0 3 double 0
DetailPrint $0
Goto end
double:
IntOp $0 $0 * 2
DetailPrint $0
end:
!macroend
!macro test2 p1
Push ${p1}
${CallArtificialFunction} testmacroasfunction
!macroend

section
!insertmacro test 2
!insertmacro test 3
!insertmacro test 4

!insertmacro test2 2
!insertmacro test2 3
!insertmacro test2 4
sectionend

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM