Re: [cc65] #include

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2003-01-09 22:52:56


Hi!

On Thu, Jan 09, 2003 at 02:34:15PM -0600, Brian Bagnall wrote:
> Forgive the newby question, but I'm having problems using include with a
> ridiculously basic object file I made. This example should call a method
> from a separate file. The main file is this:
>
> #include <stdio.h>
> #include "ia.h"
>
> int main(void)
> {
>  int x;
>  x = getnumber();
>  printf("The number is %i",x);
> }
>
> ia.h looks like this:
>
> extern int getnumber(void);
>
> ia.c looks like this:
>
> int getnumber(void)
> {
>  return 45;
> }
>
> Here's the steps I went through to build an executable:
> 1. cc65 on ia.c to produce ia.s
> 2. ca65 on ia.s to produce ia.o
> 3. saved ia.h in same directory as the rest
> 4. compiles main.h with cl65.

In step 4., you have to specify io.o so the linker can bind it to the
executable. Even easier, you can combine steps 1-4 into one:

        cl65 main.c ia.c

This will compile main.c into main.o, ia.c into ia.o and then link both
together creating an executable that has the same base name as the first file
(main in this case).

You seem to be confusing the linker step, and "#incuding" a header file. Using
#include means that the directive is replaced by the contents of the specified
file. Include files usually contain declarations like:

        extern int getnumber(void);

This differs from a definition:

        int getnumber(void)
        {
         return 45;
        }

A declaration just tells the compiler: There is an object with that name
somewhere. In case of a function, it enables the compiler to check the
parameter list, generate a subroutine call for the function, and mark the
function as "extern" in the assembler file, so an import is generated by the
object file. It is the job of the linker in a later step to search for another
object file that exports getnumber and resolve the jsr call to actually jump
to this location.

So in your case: When compiling main.c, the compiler will include ia.h, in
which the getnumber() function is declared. But since the compiler does not
see ia.c (where the function is defined), it cannot generate code for this
function, so it inserts a jsr to this function and marks this jsr so the
linker can later fill in the correct address for getnumber. To do that, the
linker needs all object files, otherwise it doesn't know the address of
getnumber.

> Unresolved external `_getnumber' referenced in:
>   C:\Projects\Play\hist.s(11)
                     ^^^^^^

This seems to be a third file?!?

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2003-01-09 22:53:03 CET