Crystal: Static compile libraries

Created on 13 Mar 2018  路  6Comments  路  Source: crystal-lang/crystal

Reproducible code:

# file: src/test.cr
puts "aaa this is a test"

Command to compile:

$聽crystal build src/test.cr --static

Error:

/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
/usr/bin/ld: cannot find -lpcre
/usr/bin/ld: cannot find -lgc
/usr/bin/ld: cannot find -levent
collect2: error: ld returned 1 exit status

Packages:

[proximyst@proximyst:Git/test]$ pacman -Qs gc
local/gc 7.6.4-1
    A garbage collector for C and C++
[proximyst@proximyst:Git/test]$ pacman -Qs ssl
local/lib32-openssl 1:1.1.0.g-1
    The Open Source toolkit for Secure Sockets Layer and Transport Layer Security (32-bit)
local/openssl 1.1.0.g-1
    The Open Source toolkit for Secure Sockets Layer and Transport Layer Security
local/openssl-1.0 1.0.2.n-1
    The Open Source toolkit for Secure Sockets Layer and Transport Layer Security
[proximyst@proximyst:Git/test]$ pacman -Qs event
local/lib32-libevent 2.1.8-1
    An event notification library (32-bit)
local/libevent 2.1.8-1
    An event notification library
[proximyst@proximyst:Git/test]$ pacman -Qs pcre
local/lib32-pcre 8.41-1
    A library that implements Perl 5-style regular expressions (32-bit)
local/pcre 8.41-1
    A library that implements Perl 5-style regular expressions
local/pcre2 10.31-1
    A library that implements Perl 5-style regular expressions. 2nd version

Else:

I do acknowledge I do not have all the libraries, but the ones I have it should mark as existing. I'm quite sure I haven't missed out on anything..

All 6 comments

Arch linux doesn't include static versions of libraries in packages by default.

Hi! @RX14 how to install this static versions on arch linux? i'have compile by make command libevent, libgc, openssl, but they doesnt have .a files.

For most projects you need to explicitly enable building the static variant, usually via something like --enable-static in their configure scripts. The next cavehat then is that the linker prefers dynamic libraries over static ones if both are in its path.

@jhass fedora 28 either doesn't have static libs

[root@tech4-fedora src]# sudo crystal build agentnnmd.cr --release --static
/usr/bin/ld: cannot find -lz
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
/usr/bin/ld: cannot find -lpcre
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lpthread
/usr/bin/ld: cannot find -levent
/usr/bin/ld: cannot find -lrt
/usr/bin/ld: cannot find -ldl
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc "${@}" -o '/root/projects/monitor/agents/linux/agentnnmd-crystal/src/agentnnmd'  -rdynamic -static  -lz `command -v pkg-config > /dev/null && pkg-config --libs libssl || printf %s '-lssl -lcrypto'` `command -v pkg-config > /dev/null && pkg-config --libs libcrypto || printf %s '-lcrypto'` -lpcre -lm -lgc -lpthread /usr/share/crystal/src/ext/libcrystal.a -levent -lrt -ldl -L/usr/lib -L/usr/local/lib`
[root@tech4-fedora src]# hostnamectl
   Static hostname: tech4-fedora.domain.local
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 6177399b289e4a219f522270533e469f
           Boot ID: fff7dc162be342858492c3d78fa8a0f9
    Virtualization: oracle
  Operating System: Fedora 28 (Twenty Eight)
       CPE OS Name: cpe:/o:fedoraproject:fedora:28
            Kernel: Linux 4.16.2-300.fc28.x86_64
      Architecture: x86-64
[root@tech4-fedora src]# 

THANKS! I'll try it

@jhass a then tryng to build with --link-flags "-static -L/path/to/libevent" ? I consciously work with static compiling first time in my life, in golang all linking perform automatically

@foi I've made a Docker image for static compilation if you are interested: https://store.docker.com/community/images/jrei/crystal-alpine

Yeah, the only way to use --static is on alpine Linux.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jhass picture jhass  路  3Comments

oprypin picture oprypin  路  3Comments

lbguilherme picture lbguilherme  路  3Comments

asterite picture asterite  路  3Comments

cjgajard picture cjgajard  路  3Comments