4

When i try to connect to DB(Postgres), via this driver https://github.com/will/crystal-pg,

require "pg"
cnn = PG.connect("postgres://root:password@localhost/my_db_dev")

i get error during compilation

Undefined symbols for architecture arm64:
  "_EVP_MD_size", referenced from:
      _*OpenSSL::Digest#digest_size:Int32 in O-penS-S-L-5858D-igest.o
      _*Digest::MD5@OpenSSL::Digest#digest_size:Int32 in D-igest5858M-D-5.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: execution of command failed with code: 1: `cc "${@}" -o /Users/serg/Pro/demka/demka  -rdynamic -L/opt/homebrew/lib -lz `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libssl || printf %s '-lssl -lcrypto'` `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libcrypto || printf %s '-lcrypto'` -lpcre -lgc -lpthread -L/opt/homebrew/Cellar/libevent/2.1.12/lib -levent -liconv -ldl`

crystal -v

Crystal 1.2.1 (2021-10-21)

LLVM: 11.1.0
Default target: aarch64-apple-darwin21.1.0

The same error happens with LLVM 10.0. It is possible to develop with crystal on the macbook m1? On my previous laptop with ubuntu, i had not errors like that.

Also, i tried making crystal from sources and got the same error

Using /opt/homebrew/opt/llvm@11/bin/llvm-config [version=11.1.0]
CRYSTAL_CONFIG_BUILD_COMMIT="5c6685cc7" CRYSTAL_CONFIG_PATH='$ORIGIN/../share/crystal/src' SOURCE_DATE_EPOCH="1636378125" CRYSTAL_CONFIG_LIBRARY_PATH='$ORIGIN/../lib/crystal' ./bin/crystal build  -o .build/crystal src/compiler/crystal.cr -D without_openssl -D without_zlib
Undefined symbols for architecture arm64:
  "_iconv", referenced from:
      _*IO::Decoder#read<IO::FileDescriptor+>:Nil in _main.o
      _*IO::FileDescriptor@IO#write_string<Slice(UInt8)>:Nil in _main.o
  "_iconv_close", referenced from:
      _~proc3Proc(Pointer(Void), Pointer(Void), Nil)@src/gc/boehm.cr:172 in _main.o
      _~proc5Proc(Pointer(Void), Pointer(Void), Nil)@src/gc/boehm.cr:172 in _main.o
  "_iconv_open", referenced from:
      _*Crystal::Iconv::new<String, String, (Symbol | Nil)>:Crystal::Iconv in _main.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: execution of command failed with code: 1: `cc "${@}" -o /Users/serg/.cache/crystal/Users-serg-Apps-crystal-src-ecr-process.cr/macro_run  -rdynamic -L/Users/serg/Downloads/crystal-1.2.1-1/embedded/lib -L/Users/serg/Downloads/crystal-1.2.1-1/embedded/lib -lpcre -lgc -lpthread -L/opt/local/lib -levent -liconv -ldl`
make: *** [.build/crystal] Error 1

And i tried making it with rosetta, there is the same error

Using /opt/homebrew/opt/llvm@11/bin/llvm-config [version=11.1.0]
CRYSTAL_CONFIG_BUILD_COMMIT="5c6685cc7" CRYSTAL_CONFIG_PATH='$ORIGIN/../share/crystal/src' SOURCE_DATE_EPOCH="1636378125" CRYSTAL_CONFIG_LIBRARY_PATH='$ORIGIN/../lib/crystal' ./bin/crystal build  -o .build/crystal src/compiler/crystal.cr -D without_openssl -D without_zlib
ld: warning: ignoring file /opt/local/lib/libiconv.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
Undefined symbols for architecture x86_64:
  "_iconv", referenced from:
      _*IO::Decoder#read<IO::FileDescriptor+>:Nil in _main.o
      _*IO::FileDescriptor@IO#write_string<Slice(UInt8)>:Nil in _main.o
  "_iconv_close", referenced from:
      _~proc3Proc(Pointer(Void), Pointer(Void), Nil)@src/gc/boehm.cr:172 in _main.o
      _~proc5Proc(Pointer(Void), Pointer(Void), Nil)@src/gc/boehm.cr:172 in _main.o
  "_iconv_open", referenced from:
      _*Crystal::Iconv::new<String, String, (Symbol | Nil)>:Crystal::Iconv in _main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: execution of command failed with code: 1: `cc "${@}" -o /Users/serg/.cache/crystal/Users-serg-Apps-crystal-src-ecr-process.cr/macro_run  -rdynamic -L/Users/serg/Downloads/crystal-1.2.1-1/embedded/lib -L/Users/serg/Downloads/crystal-1.2.1-1/embedded/lib -lpcre -lgc -lpthread -L/opt/local/lib -levent -liconv -ldl`
make: *** [.build/crystal] Error 1

I do not want to make the crystal from the source, I thought that solve my problem, I just would like to get a working crystal.

So, now i have crystal from brew installation

Crystal 1.2.1 (2021-10-21)
LLVM: 11.1.0
Default target: aarch64-apple-darwin21.1.0

And also, i still get error during compilation

Undefined symbols for architecture arm64:
  "_EVP_MD_size", referenced from:
      _*OpenSSL::Digest#digest_size:Int32 in O-penS-S-L-5858D-igest.o
      _*Digest::MD5@OpenSSL::Digest#digest_size:Int32 in D-igest5858M-D-5.o
  "_iconv", referenced from:
      _*Crystal::Iconv#convert<Pointer(Pointer(UInt8)), Pointer(UInt64), Pointer(Pointer(UInt8)), Pointer(UInt64)>:UInt64 in C-rystal5858I-conv.o
  "_iconv_close", referenced from:
      _*Crystal::Iconv#close:Nil in C-rystal5858I-conv.o
  "_iconv_open", referenced from:
      _*Crystal::Iconv#initialize<String, String, (Symbol | Nil)>:Nil in C-rystal5858I-conv.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: execution of command failed with code: 1: `cc "${@}" -o /Users/serg/.cache/crystal/crystal-run-demka.tmp  -rdynamic -L/opt/homebrew/lib -lz `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libssl || printf %s '-lssl -lcrypto'` `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libcrypto || printf %s '-lcrypto'` -lpcre -lgc -lpthread -L/opt/local/lib -levent -liconv -ldl`

all next envs are empty

$CRYSTAL_LIBRARY_PATH  #empty
$LIBRARY_PATH          #empty
$PKG_CONFIG_PATH       #empty

But command crystal env

CRYSTAL_CACHE_DIR=/Users/serg/.cache/crystal
CRYSTAL_PATH=lib:/opt/homebrew/Cellar/crystal/1.2.1/libexec/../share/crystal/src:/opt/homebrew/Cellar/crystal/1.2.1/src
CRYSTAL_VERSION=1.2.1
CRYSTAL_LIBRARY_PATH=/opt/homebrew/lib
CRYSTAL_OPTS=''

How can be fixed OpenSSL linking?

  • 1
    This looks like an OpenSSL version mismatch between compile and link time. How did you install Crystal, what's your `$CRYSTAL_LIBRARY_PATH`, `$LIBRARY_PATH`, `$PKG_CONFIG_PATH` looking like? – Jonne Haß Nov 09 '21 at 09:58
  • @JonneHaß, i try to make the crystal from sources, and i see next output `Using /opt/homebrew/opt/llvm@11/bin/llvm-config [version=11.1.0] CRYSTAL_CONFIG_BUILD_COMMIT="5c6685cc7" CRYSTAL_CONFIG_PATH='$ORIGIN/../share/crystal/src' SOURCE_DATE_EPOCH="1636378125" CRYSTAL_CONFIG_LIBRARY_PATH='$ORIGIN/../lib/crystal' ./bin/crystal build -o .build/crystal src/compiler/crystal.cr -D without_openssl -D without_zlib Undefined symbols for architecture arm64:` – Деревянко Сергей Nov 09 '21 at 10:09
  • @JonneHaß, Also, i tried making crystal via rosetta, and i got the same error `ld: warning: ignoring file /opt/local/lib/libiconv.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64 Undefined symbols for architecture x86_64` – Деревянко Сергей Nov 09 '21 at 10:10
  • It would be good to focus on a single issue at a time. Also comments here don't make for a good conversation, updating the initial question is better. As evidenced here by the fact that in both of your comments the interesting part is cut off right at the end of it :) – Jonne Haß Nov 09 '21 at 10:17
  • @JonneHaß, Ok, i have added logs – Деревянко Сергей Nov 09 '21 at 10:25
  • 1
    Which question do you want answered in this post? How to build Crystal from sources or how to troubleshooting OpenSSL linking issues? Please focus on only one topic per post. – Jonne Haß Nov 09 '21 at 10:37
  • @JonneHaß, sorry, my bad, i focused the question. – Деревянко Сергей Nov 09 '21 at 11:17

2 Answers2

2

Make sure you have the latest crystal package from Homebrew as well as the openssl@1.1 package:

brew upgrade
brew install crystal openssl@1.1

Ensure the Homebrew package's wrapper is in your path:

$ which crystal
/opt/homebrew/bin/crystal

that you're not setting an explicit PKG_CONFIG_PATH and that the wrapper properly sets it:

$ echo $PKG_CONFIG_PATH

$ grep PKG $(which crystal)
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH:+$PKG_CONFIG_PATH:}/opt/homebrew/opt/openssl@1.1/lib/pkgconfig

For building Crystal from source you'll need all the dependencies installed. From the error message it sounds like libiconv is missing or can't be found.

Jonne Haß
  • 4,792
  • 18
  • 30
1

I solved problem by add /usr/lib to CRYSTAL_LIBRARY_PATH. i just added export CRYSTAL_LIBRARY_PATH=/opt/homebrew/lib:/usr/lib to ~/.zprofile and now crystal works correct