~sircmpwn/hare-users

7 5

Can C libraries be used in Hare code?

Details
Message ID
<CXLR7L7AIFZJ.2BFAA839UFKDP@mxsr.de>
DKIM signature
missing
Download raw message
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
Details
Message ID
<CAO3qA0nwfnbu3og01_SZ72jEEZ1cFxsf+3kezCLnayeCYU+tWg@mail.gmail.com>
In-Reply-To
<CXLR7L7AIFZJ.2BFAA839UFKDP@mxsr.de> (view parent)
DKIM signature
missing
Download raw message
Yes, you can. A few examples are:

- https://git.sr.ht/~blainsmith/hare-sqlite
- https://git.sr.ht/~sircmpwn/hare-sdl2
Details
Message ID
<3a148e964c443304aea3540eb7006edc@disroot.org>
In-Reply-To
<CXLR7L7AIFZJ.2BFAA839UFKDP@mxsr.de> (view parent)
DKIM signature
missing
Download raw message
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
Details
Message ID
<CXLU03XZT8QF.2QHJ37NPWJRT2@mxsr.de>
In-Reply-To
<3a148e964c443304aea3540eb7006edc@disroot.org> (view parent)
DKIM signature
missing
Download raw message
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!
Details
Message ID
<CXMM2T5RANHZ.2VSL0N1Q01GGE@d2evs.net>
In-Reply-To
<CXLU03XZT8QF.2QHJ37NPWJRT2@mxsr.de> (view parent)
DKIM signature
missing
Download raw message
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
Details
Message ID
<CXMNIN8SE3BJ.2PUBVV08OR4C@mxsr.de>
In-Reply-To
<CXMM2T5RANHZ.2VSL0N1Q01GGE@d2evs.net> (view parent)
DKIM signature
missing
Download raw message
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.
Details
Message ID
<CXMNKMPXCE9K.2CJHEZEWMRRK7@mxsr.de>
In-Reply-To
<3a148e964c443304aea3540eb7006edc@disroot.org> (view parent)
DKIM signature
missing
Download raw message
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;
Details
Message ID
<CXMPPVFCH4JU.3P7G8GHSN93S7@smlavine.com>
In-Reply-To
<CXMNKMPXCE9K.2CJHEZEWMRRK7@mxsr.de> (view parent)
DKIM signature
missing
Download raw message
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
Reply to thread Export thread (mbox)