Zulip Chat Archive
Stream: general
Topic: FFI
Conor Bergin (Mar 03 2024 at 19:25):
Is it possible return a lean string from a cpp function without wrapping it in an IO monad? Every thing I try produces errors to do with symbols not being found. This works :
extern "C" lean_obj_res my_string() {
return lean_io_result_mk_ok(lean_mk_string("hello"));
}
with:
@[extern "my_string"] opaque myString : IO String
Can I just return a string?
Marcus Rossel (Mar 03 2024 at 20:13):
You should be able to just use lean_mk_string
and remove the IO
. What errors are you getting?
Tomas Skrivan (Mar 03 2024 at 21:40):
I think the issue is that @[extern "my_string"] opaque myString : String
does not have any arguments. Compiler considers it to be a constant that is supposed to be set during initialization and that does not seem to play well with FFI.
If you call lean myString.lean -c myString.c
on
myString.lean
@[extern "my_string"]
opaque myString : String
It generates
code
You can see that it is trying to initialize myString
with
l_myString = _init_l_myString();
lean_mark_persistent(l_myString);
When you write
@[extern "my_string"] opaque myString : BaseIO String
Then for the compiler the function is effectively Unit → String×Unit
and it won't try to initialize it as a constant.
I would consider this as a bug. A solution to it would be to define your function as:
@[extern "my_string_thunk"] opaque myStringThunk (_ : Unit) : String
def myString := myStringThunk ()
and on C side
lean_object* my_string_thunk(lean_object*){
return lean_mk_string("hello");
}
(I didn't test this fully so there might be a detail I missed)
Solomon (Mar 04 2024 at 05:13):
I'm not sure if this should go in a new thread, but I'm trying to use the C FFI to callout to libcurl
. pkg-config
is showing the library in scope:
➜ pkg-config --list-all | grep libcurl
libcurl libcurl - Library to transfer files with ftp, http, etc.
But its not getting picked up by Lean:
libc++abi: terminating due to uncaught exception of type lean::exception: error loading library, ./.lake/build/lib/libleanffi.so: undefined symbol: curl_global_cleanup
Any help would be appreciated.
EDIT:
Looks like I just needed to add moreLinkArgs := #["-lcurl"]
to my package declaration.
Jürgen Bergmann (Mar 04 2024 at 10:36):
@Solomon , please look at my Lean bindings package to libcurl https://github.com/bergmannjg/leanCurl
Solomon (Mar 04 2024 at 15:22):
Jürgen Bergmann said:
Solomon , please look at my Lean bindings package to libcurl https://github.com/bergmannjg/leanCurl
Oh this looks great. Why don't you have it listed on https://reservoir.lean-lang.org/ ?
Tomas Skrivan (Mar 04 2024 at 16:41):
There is lower bound on starts to be listed on reservoir. Two stars might not be enough and there might be other necessary metric.
Jürgen Bergmann (Mar 04 2024 at 16:57):
The metric is a root lakefile.lean, 2 stars and a OSI approved licence How Reservoir boosts your Lean packages
Last updated: May 02 2025 at 03:31 UTC