苹果硅M1上的Crystal编程语言

5

当我尝试通过这个驱动程序https://github.com/will/crystal-pg连接到DB(Postgres)时,出现了问题。

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

编译过程中出现错误

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

LLVM 10.0也会出现相同的错误。在MacBook M1上开发Crystal是否可行?在我的之前使用Ubuntu的笔记本电脑上,我没有遇到这样的错误。
另外,我尝试从源代码构建Crystal,但仍然出现了相同的错误。
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

我尝试使用Rosetta制作它,但出现了相同的错误。

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

我不想从源代码开始制作水晶,我认为解决我的问题只需要获得一个可用的水晶。

所以,现在我已经通过brew安装获得了水晶。

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

并且,我在编译过程中仍然遇到错误。
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`

所有下一个环境都是空的

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

但是命令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=''

如何修复OpenSSL链接问题?

1
这似乎是编译和链接时OpenSSL版本不匹配的问题。你是如何安装Crystal的?你的$CRYSTAL_LIBRARY_PATH$LIBRARY_PATH$PKG_CONFIG_PATH是什么样子的? - Jonne Haß
@JonneHaß,我试图从源代码构建Crystal,并看到以下输出: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: - Деревянко Сергей
@JonneHaß,另外,我尝试通过Rosetta制作水晶,但是我得到了相同的错误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 - Деревянко Сергей
一次只关注一个问题会更好。此外,在这里发表评论并不会产生良好的对话,更新初始问题会更好。正如在您的两个评论中所证明的那样,有趣的部分都被截断了 :) - Jonne Haß
1
你在这篇文章中想要回答哪个问题?是如何从源代码构建Crystal,还是如何解决OpenSSL链接问题?请每篇文章只关注一个主题。 - Jonne Haß
显示剩余2条评论
2个回答

2

确保你已经从Homebrew获取了最新的Crystal包,并且安装了openssl@1.1包:

brew upgrade
brew install crystal openssl@1.1

确保Homebrew包装程序的路径在您的系统路径中:

$ which crystal
/opt/homebrew/bin/crystal

请确保您没有设置明确的PKG_CONFIG_PATH,并且包装器正确地设置了它:

$ 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

从源代码构建Crystal需要安装所有依赖项。根据错误信息,似乎缺少libiconv或找不到它。

1

我通过将/usr/lib添加到CRYSTAL_LIBRARY_PATH中解决了问题。 我只是在~/.zprofile中添加了export CRYSTAL_LIBRARY_PATH=/opt/homebrew/lib:/usr/lib,现在Crystal工作正常了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接