Hi all,
can C libraries be used in Hare code? I can't find any examples for this
and IIRC, I read somewhere that it isn't supported but I can't find a
confirmation for this. On the other hand, these flags in `man
hare-build` look like C libraries are supported:
-L libdir
Add a directory to the linker library search path.
-l libname
Link with the named system library. The name is passed directly to the linker. Linking with any library will also link with libc(7) and add the +libc tag to
the default build tags (see BUILD TAGS in hare-module(5)).
Could somebody clarify this?
I'm wondering if it's possible to embed Lua/LuaJIT in a Hare program.
Best regards,
Max
On 2023-12-11 20:33, Max Schillinger wrote:
> Hi all,> > can C libraries be used in Hare code? I can't find any examples for > this> and IIRC, I read somewhere that it isn't supported but I can't find a> confirmation for this. On the other hand, these flags in `man> hare-build` look like C libraries are supported:> > -L libdir> Add a directory to the linker library search path.> > -l libname> Link with the named system library. The name is passed directly > to the linker. Linking with any library will also link with libc(7) and > add the +libc tag to> the default build tags (see BUILD TAGS in hare-module(5)).> > Could somebody clarify this?> > I'm wondering if it's possible to embed Lua/LuaJIT in a Hare program.> > Best regards,> Max
You can link against the object file.
Consider the following example:
You have a add.c file with following content:
int add(int x, int y) {
return x+y;
}
In your hare code you can define the symbol using:
@symbol("add") export fn add(x: int, y: int);
When compiling you have to have the object file of your C program in the
same directory as your hare program.
Use hare build -lc to compile against your object file.
Greetings
Simon
On Mon Dec 11, 2023 at 8:49 PM CET, wrote:
> You can link against the object file.
OK, then this is the restriction. Then no static libraries (*.a) either?
> Consider the following example:>>> You have a add.c file with following content:> int add(int x, int y) {> return x+y;> }>>> In your hare code you can define the symbol using:> @symbol("add") export fn add(x: int, y: int);>>> When compiling you have to have the object file of your C program in the > same directory as your hare program.> Use hare build -lc to compile against your object file.
Thank you!
On Mon Dec 11, 2023 at 8:37 PM CET, Blain Smith wrote:
> Yes, you can. A few examples are:>> - https://git.sr.ht/~blainsmith/hare-sqlite> - https://git.sr.ht/~sircmpwn/hare-sdl2
Thank you for the examples!
On Mon Dec 11, 2023 at 9:44 PM UTC, Max Schillinger wrote:
> On Mon Dec 11, 2023 at 8:49 PM CET, wrote:> > You can link against the object file.>> OK, then this is the restriction. Then no static libraries (*.a) either?
object files in a module are linked in by default, and you can also link
in c libraries with -l, which is (currently( equivalent to passing the
same -l to cc
On Tue Dec 12, 2023 at 8:44 PM CET, Ember Sawady wrote:
> > OK, then this is the restriction. Then no static libraries (*.a) > > either?>> object files in a module are linked in by default, and you can also link> in c libraries with -l, which is (currently( equivalent to passing the> same -l to cc
Thank you. Now I got it working for either an .o, an .a or an .so file.
On Mon Dec 11, 2023 at 8:49 PM CET, wrote:
> In your hare code you can define the symbol using:> @symbol("add") export fn add(x: int, y: int);
For future readers:
I had to change this line to:
@symbol("add") fn add(x: int, y: int) int;
On Tue Dec 12, 2023 at 3:54 PM EST, Max Schillinger wrote:
> On Mon Dec 11, 2023 at 8:49 PM CET, wrote:> > In your hare code you can define the symbol using:> > @symbol("add") export fn add(x: int, y: int);>> For future readers:>> I had to change this line to:>> @symbol("add") fn add(x: int, y: int) int;
The `export` has to come before the `@symbol`.
In my limited experience though, it is better not to directly expose
foreign symbols to Hare users; you'll quite often want to do some
wrapping on the Hare side to take care of things like mapping sentinel
values to error types and other similar things.
--
Sebastian LaVine