简体   繁体   English

编译错误,因为我两次调用宏

[英]Compile Error because I call a Macro twice

When I call a macro function twice (inside a section) I get this compile error: 当我两次(在部分中)调用宏函数时,出现此编译错误:

Error: label "CheckForRMSCustomisationLoop:" already declared in section 错误:已经在本节中声明的标签“ CheckForRMSCustomisationLoop:”

I understand its because I am defining a label(jump) twice, is that correct? 我理解它是因为我两次定义了一个标签(跳转),对吗?

How can I avoid this problem? 如何避免这个问题?

Do I HAVE to convert the macro to a function or is there an easier way? 我必须将宏转换为函数还是有更简单的方法? Because converting to a function means I cant pass parameters I have to use the stack. 因为转换为函数意味着我无法传递参数,所以我必须使用堆栈。

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
*/

You're right, you're defining labels TestLabel1 and TestLabel2 twice. 没错,您要两次定义标签TestLabel1TestLabel2

You could make your label names unique using this method: 您可以使用以下方法使标签名称唯一:

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

More information here: 此处的更多信息:

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

It appears this is a solution and its pretty elegant too: 看来这是一个解决方案,它也很优雅:

!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