~sircmpwn/hare-users

3 2

Linking error when mixing C and Hare objects

Details
Message ID
<D6TM7NHDQ5WX.3G4PEB0AH99DA@mxsr.de>
DKIM signature
pass
Download raw message
I would like to convert a single C file (translation unit) to Hare.

main.c:

    #include "rect.h"
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
    	init_rectangle(20, 10);
    	float area = get_rectangle_size();
    	printf("area: %f\n", area);
    	return 0;
    }


rect.h:

    typedef struct {
    	float w, h;
    } Rectangle;
    
    extern Rectangle rectangle;
    
    void init_rectangle(float w, float h);
    float get_rectangle_size(void);


rect.c:

    #include "rect.h"
    
    Rectangle rectangle;
    
    void init_rectangle(float w, float h) {
    	rectangle.w = w;
    	rectangle.h = h;
    }
    
    float get_rectangle_size(void) {
    	return rectangle.w * rectangle.h;
    }


I have replaced `rect.c` with `rect.ha`:

    export type Rectangle = struct { w: f32, h: f32 };
    
    export let @symbol("rectangle") rectangle: Rectangle;
    
    export @symbol("init_rectangle") fn init_rectangle(w: f32, h: f32) void = {
    	rectangle.w = w;
    	rectangle.h = h;
    };

    export @symbol("get_rectangle_size") fn get_rectangle_size() f32 = {
    	return rectangle.w * rectangle.h;
    };


$ hare build -t o -o rect_hare.o rect.ha
$ cc -c main.c
$ cc -o main main.o rect_hare.o
/usr/bin/ld: rect_hare.o: warning: relocation against `rectangle' in read-only section `.text.init_rectangle'
/usr/bin/ld: rect_hare.o: in function `get_rectangle_size':
/media/home/max/dev/hare/c2hare/rect.ha:10:(.text.get_rectangle_size+0x8): undefined reference to `rectangle'
/usr/bin/ld: /media/home/max/dev/hare/c2hare/rect.ha:10:(.text.get_rectangle_size+0x10): undefined reference to `rectangle'
/usr/bin/ld: rect_hare.o: in function `init_rectangle':
/media/home/max/dev/hare/c2hare/rect.ha:6:(.text.init_rectangle+0x8): undefined reference to `rectangle'
/usr/bin/ld: /media/home/max/dev/hare/c2hare/rect.ha:7:(.text.init_rectangle+0x10): undefined reference to `rectangle'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status


Can anybody tell me what's wrong?
Details
Message ID
<D6TN9VC7G2RJ.1LAIJVAHJC1Y@disroot.org>
In-Reply-To
<D6TM7NHDQ5WX.3G4PEB0AH99DA@mxsr.de> (view parent)
DKIM signature
pass
Download raw message
On Sat Jan 4, 2025 at 4:43 PM EST, Max Schillinger wrote:
>     export let @symbol("rectangle") rectangle: Rectangle;

Because you have't initialized the variable here, the linker will search
for the symbol somewhere else in another file compiled into the program.
You need to initialize with something:

  export let @symbol("rectangle") rectangle = Rectangle { ... };
Details
Message ID
<D6TNL92UOP07.3O8DN1VJD97X7@mxsr.de>
In-Reply-To
<D6TN9VC7G2RJ.1LAIJVAHJC1Y@disroot.org> (view parent)
DKIM signature
pass
Download raw message
On Sat Jan 4, 2025 at 11:33 PM CET, Luna wrote:
> On Sat Jan 4, 2025 at 4:43 PM EST, Max Schillinger wrote:
> >     export let @symbol("rectangle") rectangle: Rectangle;
>
> Because you have't initialized the variable here, the linker will search
> for the symbol somewhere else in another file compiled into the program.
> You need to initialize with something:
>
>   export let @symbol("rectangle") rectangle = Rectangle { ... };

That's it. Now it works. Thanks a lot for your help!
Details
Message ID
<D6TNOKN24H6W.3FIB1U11C2803@disroot.org>
In-Reply-To
<D6TNL92UOP07.3O8DN1VJD97X7@mxsr.de> (view parent)
DKIM signature
pass
Download raw message
On Sat Jan 4, 2025 at 5:48 PM EST, Max Schillinger wrote:
> On Sat Jan 4, 2025 at 11:33 PM CET, Luna wrote:
> > On Sat Jan 4, 2025 at 4:43 PM EST, Max Schillinger wrote:
> > >     export let @symbol("rectangle") rectangle: Rectangle;
> >
> > Because you have't initialized the variable here, the linker will search
> > for the symbol somewhere else in another file compiled into the program.
> > You need to initialize with something:
> >
> >   export let @symbol("rectangle") rectangle = Rectangle { ... };
>
> That's it. Now it works. Thanks a lot for your help!

You're welcome!
Reply to thread Export thread (mbox)