Crystal --static says cannot find -lpcre

Hi,

This is my first post. So don’t be too hard on me :grin:
I have Artix Linux (a fork of Arch Linux with OpenRC as PID 1). I try crystal. All is perfect, except when I try to build a static executable:

$ crystal build src/server.cr --release --static
/usr/bin/ld: cannot find -lpcre (this usually means you need to install the development package for libpcre)
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc "${@}" -o /home/xan/zync/projectes/wikid-c/server  -rdynamic -static -lpcre -lm -lgc -lpthread -levent  -lrt -ldl`

I search pcre packages in artix and I have all I need, I think:

$ pacman -Ss pcre
system/pcre 8.45-1 [instal·lat]
    A library that implements Perl 5-style regular expressions
system/pcre2 10.37-1 [instal·lat]
    A library that implements Perl 5-style regular expressions. 2nd version
world/postfix-pcre 3.6.2-1
    Fast, easy to administer, secure mail server (PCRE integration)
lib32/lib32-pcre 8.44-1
    A library that implements Perl 5-style regular expressions (32-bit)
lib32/lib32-pcre2 10.37-1
    A library that implements Perl 5-style regular expressions. 2nd version
    (32-bit)
extra/postfix-pcre 3.6.2-1
    Fast, easy to administer, secure mail server (PCRE integration)
community/gambas3-gb-pcre 3.16.2-5 (gambas3)
    PCRE component
community/haskell-pcre-heavy 1.0.0.2-181
    A regexp library on top of pcre-light you can actually use
community/haskell-pcre-light 0.4.1.0-122
    Portable regex library for Perl 5 compatible regular expressions
community/haskell-regex-pcre 0.95.0.0-10
    PCRE Backend for Text.Regex (regex-base)
multilib/lib32-pcre 8.44-1
    A library that implements Perl 5-style regular expressions (32-bit)
multilib/lib32-pcre2 10.37-1
    A library that implements Perl 5-style regular expressions. 2nd version
    (32-bit)

Can someone please help me?

Thanks in advance,

My understanding is you need a .a static lib file for pcre but that file is not included with the pcre2 package. Easiest solution for creating static binaries is using alpine linux.

See https://crystal-lang.org/reference/guides/static_linking.html#fully-static-linking

Thanks @Blacksmoke16 but I want to use my own distribution and glibc. I know musl would not make errors, but I want to build static with glibc

You should be able to link libpcre statically, assuming you have the respective library (libpcre.a) available and prioritized by the linker.
Please refer to Static Linking - Crystal for instructions.

I’m afraid you probably won’t be able to produce a fully statically linked binary with glibc.

Why not I won’t be able to do this with glibc?

glibc dynamically links to libraries it needs as it needs them, so there is no portability in the resulting “statically linked” binary unless you use musl

3 Likes

@stakach OK. So I can’t use glibc. But what about using musl with my own distro, without having to install alpine linux in a virtual machine. It’s at least a reasonable petition?

Easiest solution would be to use the Alpine Docker image: Alpine-based Docker images - The Crystal Programming Language.

Please, stop refering to official documentation. I read it. The only solution it provides is to install Alpine Docker Image. Yes I know. But I want to compile a simple program in my own distro (not installing a new one). So is there any solution there or the conclusion is that Crystal only compiles statically in Alpine Linux?

If you can get a hold of the respective static libraries on your distro and convince the linker to link against them instead of the system libc, sure, this should probably work.
Please note that you need all libraries (such as libgc or libpcre) to be built for linking against musl. You can’t use the standard libraries from your glibc-based distro because they expect to be linked against glibc.

This is a very impractical approach and I don’t think I have ever heard about anyone trying that.
But I’m happy to hear about your findings.