I'm mixing Hare and C objects again.
`layout.ha` contains this function:
export @symbol("layout_row_render") fn layout_row_render(
row: nullable *LayoutRow,
) void = {
match (row) {
case null => void;
case let row: *LayoutRow =>
let x: f32 = row.x;
let widget: nullable *Widget = row.widgets;
for (widget is *Widget) {
let r: Rectangle = render_texture(
(widget as *Widget).texture,
x,
row.y,
(widget as *Widget).width,
(widget as *Widget).height,
ALIGN_TOP_LEFT);
x += r.w + row.spacing;
widget = (widget as *Widget).next;
};
};
};
`hare compile -t o` works fine.
The linker complains:
/usr/bin/ld: layout.o: in function `layout_row_render':
/media/home/max/repos/TinyTicTacToe-Hare/app/src/main/jni/layout.ha:135:(.text.layout_row_render+0x36): undefined reference to `rt.abort_fixed'
(Line 135 is the first line in the function: `match (row) {`)
Can anyone tell me what's wrong?
> The linker complains:>> /usr/bin/ld: layout.o: in function `layout_row_render':> /media/home/max/repos/TinyTicTacToe-Hare/app/src/main/jni/layout.ha:135:(.text.layout_row_render+0x36): undefined reference to `rt.abort_fixed'>> (Line 135 is the first line in the function: `match (row) {`)>> Can anyone tell me what's wrong?
Hare expects some functions to be present in the final binary and
rt.abort_fixed is one of them. You can get a good enough approximation of what
is needed by building the rt/ folder from the stdlib or the compiler repo with
`hare build -t o -o rt.o rt/`, and then you can try linking that into your
executable.
you'll need to manually link rt:: (and any other stdlib modules you end
up using) in
we haven't put a ton of work into integrating hare code into external
build systems; as of now, you'll likely have more success using hare as
the linker - `hare build` will treat any object files you stick in a
hare module's directory as inputs to be linked in, just make sure you
pass at least one -l flag in to it (-lc works if you're not using any c
libraries), to make sure that libc is linked in as well
On Sun Jan 5, 2025 at 8:55 PM CET, Bor Grošelj Simić wrote:
> > The linker complains:> >> > /usr/bin/ld: layout.o: in function `layout_row_render':> > /media/home/max/repos/TinyTicTacToe-Hare/app/src/main/jni/layout.ha:135:(.text.layout_row_render+0x36): undefined reference to `rt.abort_fixed'> >> > (Line 135 is the first line in the function: `match (row) {`)> >> > Can anyone tell me what's wrong?>> Hare expects some functions to be present in the final binary and> rt.abort_fixed is one of them. You can get a good enough approximation of what> is needed by building the rt/ folder from the stdlib or the compiler repo with > `hare build -t o -o rt.o rt/`, and then you can try linking that into your> executable.
Thank you. I have tried this but now I get a different error:
/usr/bin/ld: rt.o: in function `_start':
(.text+0x0): multiple definition of `_start'; /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib/Scrt1.o:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
On Sun Jan 5, 2025 at 8:41 PM CET, Ember Sawady wrote:
> you'll need to manually link rt:: (and any other stdlib modules you end > up using) in>> we haven't put a ton of work into integrating hare code into external > build systems; as of now, you'll likely have more success using hare as > the linker - `hare build` will treat any object files you stick in a > hare module's directory as inputs to be linked in, just make sure you > pass at least one -l flag in to it (-lc works if you're not using any c > libraries), to make sure that libc is linked in as well
Thank you for the hint! When I try this, I get a new error:
layout.ha:38:7: error: Duplicate global ident '::Alignment'
38 | export type Alignment = uint;
| ^
This `Alignment` part was missing in my original snippet. These lines
are also contained in `layout.ha`:
export type Alignment = uint;
def ALIGN_TOP_LEFT: uint = 0;
def ALIGN_TOP_RIGHT: uint = 2;
(In C, this was a `typedef enum {...} Alignment`.)
On Sun Jan 5, 2025 at 8:05 PM UTC, Max Schillinger wrote:
> layout.ha:38:7: error: Duplicate global ident '::Alignment'
are you defining Alignment in any other .ha files in that directory?
On Sun Jan 19, 2025 at 9:19 PM CET, Ember Sawady wrote:
> On Sun Jan 5, 2025 at 8:05 PM UTC, Max Schillinger wrote:> > layout.ha:38:7: error: Duplicate global ident '::Alignment'>> are you defining Alignment in any other .ha files in that directory?
Thank you for the hint! Indeed, I was defining Alignment in two .ha
files because both files need this type. I have tried defining this type
in a single (third) file (analogous to a C header file), but then `hare
build -t o layout.ha` fails because it can't see this type. Then I
realized that I shouldn't create object files with `hare build -t o`
when I use `hare build` for linking everything together.
But this approach still fails because `main` is defined both in my
`main_x11.c` and Hare's `rt` module:
$ hare build -v -o tinytictactoe-hare -lX11 -lGLESv2 -lEGL -lm -lvorbisfile -lasound
/usr/bin/ld: /media/home/max/repos/TinyTicTacToe-Hare/app/src/main/jni/main_x11.o: in function `main':
/media/home/max/repos/TinyTicTacToe-Hare/app/src/main/jni/main_x11.c:125: multiple definition of `main'; /home/max/.cache/hare/usr/local/src/hare/stdlib/rt/7c8b86d6d6deb76da23401e676ed227df137ace9d8c003a9e4f9af9e1a1140e5.o:(.text.main+0x0): first defined here
/usr/bin/ld: /home/max/.cache/hare/usr/local/src/hare/stdlib/rt/7c8b86d6d6deb76da23401e676ed227df137ace9d8c003a9e4f9af9e1a1140e5.o: in function `main':
/media/home/max/repos/TinyTicTacToe-Hare/app/src/main/jni/rt/+linux/start+libc.ha:20:(.text.main+0x2d): undefined reference to `.main'
Can I tell Hare to ignore rt's main function?
On Mon Jan 20, 2025 at 9:53 AM UTC, Max Schillinger wrote:
> But this approach still fails because `main` is defined both in my > `main_x11.c` and Hare's `rt` module:
ah, yeah, that kinda sucks. it's a bit hacky, but try sticking an
export fn main() void = main2();
in the hare code, then renaming your c main function to main2
hare needs to intercept main(), in order to be able to eg. run @init
functions; we configure the hare toolchain to put the user's main() in
the `.main` symbol, but that doesn't work with c
On Mon Jan 20, 2025 at 2:41 PM CET, Drew DeVault wrote:
> Could also build it freestanding (-F) and add -T+libc -lc
I take that back after thinking about it for more than 5 seconds
On Mon Jan 20, 2025 at 2:40 PM CET, Ember Sawady wrote:
> On Mon Jan 20, 2025 at 9:53 AM UTC, Max Schillinger wrote:>> But this approach still fails because `main` is defined both in my >> `main_x11.c` and Hare's `rt` module:>> ah, yeah, that kinda sucks. it's a bit hacky, but try sticking an>> export fn main() void = main2();>> in the hare code, then renaming your c main function to main2>> hare needs to intercept main(), in order to be able to eg. run @init > functions; we configure the hare toolchain to put the user's main() in > the `.main` symbol, but that doesn't work with c
Thank you, it works!
For future readers with the same problem:
I had to add these _two_ lines to a .ha file:
export @symbol("main2") fn main2() void;
export fn main() void = main2();