Zulip Chat Archive

Stream: lean4

Topic: FFI on windows


James Randolf (Aug 21 2023 at 02:55):

I'm trying to use FFI on Windows and I'm getting the following error when it tries to compile ffi.c, am I doing something wrong?

fatal error: 'stddef.h' file not found
#include <stddef.h>
         ^~~~~~~~~~
1 error generated.
error: external command `clang` exited with code 1

Mac Malone (Aug 21 2023 at 03:21):

@James Randolf What does your lakefile look like (how are you compiling your FFI)?

James Randolf (Aug 21 2023 at 03:26):

import Lake
open Lake DSL

package test
lean_lib Test

@[default_target] lean_exe test {
  root := `Main
}

target ffi.o pkg : FilePath := do
  let oFile := pkg.buildDir / "c" / "ffi.o"
  let srcJob  inputFile <| pkg.dir / "c" / "ffi.c"
  let flags := #["-I", ( getLeanIncludeDir).toString]
  buildO "ffi.c" oFile srcJob flags "clang"

extern_lib libleanffi pkg := do
  let name := nameToStaticLib "leanffi"
  let ffiO  fetch <| pkg.target ``ffi.o
  buildStaticLib (pkg.nativeLibDir / name) #[ffiO]

James Randolf (Aug 21 2023 at 03:27):

ffi.c is

#include <lean/lean.h>

int test(int x) {
    return x * 2;
}

James Randolf (Aug 21 2023 at 03:44):

It seems that without the #include, everything works, but as soon as I try to include anything at all I get a fatal error: '...' file not found error.

Mac Malone (Aug 21 2023 at 03:58):

@James Randolf The error indicates it cannot find the C standard library includes (in this case, the compiler-specific stddef) in its default search path. How did you install clang?

Mac Malone (Aug 21 2023 at 04:01):

Windows does not come with a C compiler and library by default, so it is quite easy to get this error if something went wrong in the the setup of whatever compiler you do install (Clang in your case).

James Randolf (Aug 21 2023 at 04:01):

It seems to be using the clang at C:\Users\James\.elan\toolchains\leanprover--lean4---nightly-2023-08-19\bin\clang.exe

Mac Malone (Aug 21 2023 at 04:04):

@James Randolf Oh, in that case, yeah that will not work. Lean does not ship a standard library with tis bundled Clang.

Mac Malone (Aug 21 2023 at 04:04):

You need to install a C compiler yourself and use that to build.

James Randolf (Aug 21 2023 at 04:05):

I see, thanks a lot!

Mac Malone (Aug 21 2023 at 04:06):

Quick example: to install Clang in MSYS2 you can execute the following in a MSYS2 shell.

pacman -S mingw-w64-x86_64-clang

Then, as long as you run lake build from the MSYS2 shell, it should use the clang installed.


Last updated: Dec 20 2023 at 11:08 UTC