5
votes

I am using cc65 6502 simulator, which compiles code for 6502. I wish to link the 6502 code and C code and produce a binary file that I can execute.

My C code "main.c":

 #include<stdio.h>
 extern void foo(void);

 int main() {
    foo();
    return 0;
 }

My 6502 code "foo.s":

 foo:
      LDA #$00
      STA $0200

The code might seem very simple but I am just trying to achieve the successful linking. But I cannot get rid of the following error:

Unresolved external '_foo' referenced in: main.s(27) ld65: Error: 1 unresolved external(s) found - cannot create output file

1
simply decorated name in c must be exactly to called name from asm. and visa versa. if you use __cdecl as default calling convention - the decorated (full) name of foo will be _foo - and in asm file you need use _foo name instead of foo. also name must be public (visible). the label usually not. you need declare _foo as public symbol. or as function (functions usual public symbols). however this depend from concrete asm compiler. now, when you build - you got error - unresolved external symbol (_foo i guess) - exactly this name you must implement in asmRbMm
I tried naming the asm file to _foo.s and in the main.c I changed the function name to _fib, but I still cannot get rid of that error.Milap Jhumkhawala
Your .s file isn't declaring a function. foo: is just a label. In order for it to appear as a linkable symbol, you will need to add some additional (assembler-specific) directives to the file.Mark Bessey
cl65 -t sim6502 main.c _fib.s -o fib yes, this compiles the C for 6502, github.com/cc65/cc65Milap Jhumkhawala
It's even part of the intro cc65.github.io/doc/intro.html#ss1.2Polluks

1 Answers

6
votes

You need to export it from the assembly module - with the same decoration the C compiler uses:

_foo:
.export _foo
      LDA #$00
      STA $0200

This links with:

cl65 -t sim6502 main.c foo.s -o foo

You might also need to look into the calling conventions.