Using the .MACPACK directive, predefined macro packages can be included in the assembler source code with .macpack name. For example .macpack generic defines macros like add, or sub (see https://cc65.github.io/doc/ca65.html#macropackages). However, including the same macropackage twice throws an error, because the respective macros are already defined. This problem is sometimes difficult to avoid when a package is included again via a different file.

I tried to put a wrapper around the .macpack command like this:

    .ifndef add      ;checking if the add macro is already defined
    .macpack generic

but that did not work because the .ifdef / .ifndef don't trigger on macro definitions. Any ideas on how I could protect the macro package from double use? The macro package itself is part of the cc65 suite, so I don't want to change it directly.


1 Answer 1


TL;DR: Use .DEFINEDMACRO to check for a macro being defined

.IFNDEF is a short hand for .IF .NOT .DEF[INED] (*1). .DEF[INED] looks up assembler symbols, i.e. any identifier that works like a label. The name of a macro is not a an assembler symbol but a macro identifier, handled like a build in CPU mnemonic, but managed in a separate table of macro names, which can be searched by .DEFINEDMACRO.

.DEFINEDMACRO can be found under Pseudo Functions in section 10.8, right after .DEF[INED]. With .MACPACKits application might look like this:

        .if     .not .definedmacro(add)
                .macpack generic

In addition macro identifiers can be found exactly like CPU mnemonics by .ISMENM[ONIC] if the .FEATURE ubiquitous_idents is set (see 11.41) to allow redefining CPU mnemonics (*2). It's described in section 10.12 of the CA65 documentation. In fact, the very example used there is about defining a macro only if not defined already.

*1 - Obviously added to help C-addicts to have a less hart time when breaking free and discovering the beautiful world without :))

*2 - I usually set this as default.

  • Thank you very much for the suggestion, but I tried it out (ca65 V2.19) and it seems that .ismenemonic also cannot detect a macro identifier. The example in Section 10.12 does not work if the same code is pasted twice, it throws a "Macro is already defined" error on the second part.
    – Peter B.
    Sep 21, 2022 at 9:56
  • @PeterB. Oops, my fault, I got always enabled that macros can replace instructions. But no prob, use .DEFINEDMACRO instead - stands in the manual right after .DEFINED in the manual :)) I'll correct that.
    – Raffzahn
    Sep 21, 2022 at 10:59
  • 1
    .definedmacro works perfectly, thanks for your quick and competent help!
    – Peter B.
    Sep 21, 2022 at 12:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .