Crystal on WSL2 w/Homebrew?

WSL 2 on Windows 11. Installed crystal via Linuxbrew.

Attempts to run crystal run hello.cr yield linker errors:

ted@TedPad-P51:~/Projects/Exploration.git/Crystal$ crystal run hello.cr
/home/linuxbrew/.linuxbrew/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x16): undefined reference to `__libc_csu_fini'
/home/linuxbrew/.linuxbrew/bin/ld: (.text+0x1d): undefined reference to `__libc_csu_init'
collect2: error: ld returned 1 exit status
Error: execution of command failed with exit status 1: cc "${@}" -o /home/ted/.cache/crystal/crystal-run-hello.tmp  -rdynamic -L/home/linuxbrew/.linuxbrew/Cellar/crystal/1.8.2/libexec/../../../../lib -lpcre2-8 -lm -lgc -lpthread -L/home/linuxbrew/.linuxbrew/Cellar/libevent/2.1.12/lib -levent -lrt -lpthread -ldl
ted@TedPad-P51:~/Projects/Exploration.git/Crystal$

Appears to be a Crystal-specific issue, as gcc compiles and links fine when compiling other (C/C++) programs. Not clear what to check or how to debug.

Note that some slightly different errors (and warnings) appear when using --static:

ted@TedPad-P51:~/Projects/Exploration.git/Crystal$ crystal build --static hello.cr
/home/linuxbrew/.linuxbrew/bin/ld: /home/linuxbrew/.linuxbrew/Cellar/crystal/1.8.2/libexec/../../../../lib/libevent.a(evutil.o): in function `test_for_getaddrinfo_hacks':
evutil.c:(.text+0x1cc): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/home/linuxbrew/.linuxbrew/bin/ld: /home/linuxbrew/.linuxbrew/Cellar/crystal/1.8.2/libexec/../../../../lib/libevent.a(evutil.o): in function `evutil_getaddrinfo_common_':
evutil.c:(.text+0x1e8b): warning: Using 'getprotobynumber' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/home/linuxbrew/.linuxbrew/bin/ld: evutil.c:(.text+0x1f60): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/home/linuxbrew/.linuxbrew/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crt1.o: in function `_start':
(.text+0x16): undefined reference to `__libc_csu_fini'
/home/linuxbrew/.linuxbrew/bin/ld: (.text+0x1d): undefined reference to `__libc_csu_init'
collect2: error: ld returned 1 exit status
Error: execution of command failed with exit status 1: cc "${@}" -o /home/ted/Projects/Exploration.git/Crystal/hello  -rdynamic -static -L/home/linuxbrew/.linuxbrew/Cellar/crystal/1.8.2/libexec/../../../../lib -lpcre2-8 -lm -lgc -lpthread -L/home/linuxbrew/.linuxbrew/Cellar/libevent/2.1.12/lib -levent -lrt -lpthread -ldl
ted@TedPad-P51:~/Projects/Exploration.git/Crystal$

Not sure if that’s helpful or noise.

It feels like there’s something that didn’t get dropped in the right place for a Linuxbrew-based Crystal install, but I can’t be sure.

If you use Ubuntu on WSL2, why not just use the official package manager for install Crystal?

Fair question. Still, according to the status page of brew, Linux 64 should be working fine.

So any thoughts on how to debug it? It seems like libc and libevent aren’t being found in whatever default locations the compiled Crystal binary thinks they should be. I’ll happily file a bug with the Linuxbrew crystal-lang recipe, but I’d like to verify it’s a Linuxbrew bug before I do that, and i’m not sure how to do that.

I would suggest installing crystal from the official source, and if that works on the same system/install, i would argue that it’s a linuxbrew bug.

This error indicates some kind of misconfiguration in the C tool chain. AFAIK crt1.o should actually provide __libc_csu_fini itself. If it can’t be found there’s something afoul.
But I don’t think it’s a specific error of WSL2 and or Homebrew on Linux. Both work fine for me (and many others have reported the same).

So before creating a ticket anywhere, it should be helpful to figure out what is even the reason for this failure.
Some information that could be useful for that would be the values of envirnment variables like CC, LDFLAGS and LD_LIBRARY_PATH.
Please also try to compile the program with crystal build --verbose to see which linker command is actually invoked.