5

Trying to learn how to use the ca65 assembler, I've been battling with making include guards work. Googling and reading the ca65 Users Guide didn't help. Here's a minimal example that produces the error.

$ ls -l
total 16
-rw-r--r--  1 me  staff  60 Oct 22 19:40 65.inc
-rw-r--r--  1 me  staff  55 Oct 22 20:01 test.s
$
$ cat 65.inc
.ifndef _65_INC_
.define _65_INC_

.define NUMBER 1

.endif
$
$ cat test.s
.include "65.inc"
.include "65.inc"

    lda #NUMBER
    rts
$
$ ca65 test.s
65.inc(1): Error: Identifier expected
65.inc(2): Error: Identifier expected
65.inc(4): Error: Identifier expected
65.inc(4): Note: Macro was defined here
$
$ ls -l
total 16
-rw-r--r--  1 me  staff  60 Oct 22 19:40 65.inc
-rw-r--r--  1 me  staff  55 Oct 22 20:01 test.s
$

If I only include 65.inc once in test.s it assembles without problem, as seen here:

$ ls -l
total 16
-rw-r--r--  1 me  staff  60 Oct 22 19:40 65.inc
-rw-r--r--  1 me  staff  37 Oct 22 20:07 test.s
$
$ cat 65.inc
.ifndef _65_INC_
.define _65_INC_

.define NUMBER 1

.endif
$
$ cat test.s
.include "65.inc"

    lda #NUMBER
    rts
$
$ ca65 test.s
$
$ ls -l
total 24
-rw-r--r--  1 me  staff   60 Oct 22 19:40 65.inc
-rw-r--r--  1 me  staff  295 Oct 22 20:07 test.o
-rw-r--r--  1 me  staff   37 Oct 22 20:07 test.s
$
$ ca65 --version
ca65 V2.17 - Git 153bb29

What am I missing?

J. Murray
  • 1,460
  • 11
  • 19
Quester
  • 81
  • 7

1 Answers1

9

Somewhat confusingly .ifndef and friends apply to symbols which macros are not (.define defines a macro). Thus a possible workaround is to use a symbol, e.g.

.ifndef _65_INC_
_65_INC_ = 1

.define NUMBER 1

.endif
Jester
  • 56,577
  • 4
  • 81
  • 125
  • 2
    Thanks Jester, that makes sense. My code was inspired by the source in the first download link at: (https://csdb.dk/release/?id=32392) where he does use a .define I wonder why it works for him and not for me? – Quester Oct 22 '19 at 19:39
  • 4
    It doesn't work there either, it's just that the guards are never actually used since no file is included twice. As in your case, if you only include once then there is no error (but the whole exercise is unnecessary then of course). – Jester Oct 22 '19 at 19:55
  • 1
    Haha ok, even the elite are only human :) Thanks for explaining. – Quester Oct 22 '19 at 21:01