Error using BigDecimal: 'undefined reference to `__gmp_set_memory_functions' (Crystal::Loader::LoadError)'

Hi, I have just installed Crystal and am trying to use BigDecimal, and getting an error. Some details follow.

  • macOS Monterey 12.6, Apple M1
  • Crystal 1.7.0 installed with Homebrew
  • gmp 6.2.1_1 installed with Homebrew

Running the following:

$ crystal i
icr> require "big/big_decimal"

Getting error:

undefined reference to `__gmp_set_memory_functions' (Crystal::Loader::LoadError)
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'raise<Crystal::Loader::LoadError>:NoReturn'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::Repl::Context#c_function<String>:Pointer(Void)'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::Repl::Compiler#visit<Crystal::Call>:Bool'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::Repl::Compiler>:Nil'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::Repl::Compiler>:Nil'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::Repl::Compiler#compile_def:closure_owner<Crystal::Repl::CompiledDef, Crystal::FileModule>:Nil'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::Repl::Compiler>:Nil'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::Repl::Compiler>:Nil'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::Repl::Compiler#compile_def:closure_owner<Crystal::Repl::CompiledDef, Crystal::FileModule>:Nil'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::Repl::Compiler>:Nil'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::Repl::Compiler>:Nil'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::Repl::Compiler#compile_def:closure_owner<Crystal::Repl::CompiledDef, Crystal::FileModule>:Nil'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::Repl::Compiler>:Nil'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::Repl::Compiler#compile<Crystal::ASTNode+>:Nil'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::Repl::Interpreter#interpret<Crystal::ASTNode+, Hash(String, Crystal::MetaVar)>:Crystal::Repl::Value'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'Crystal::Command#repl:(Crystal::Repl::Value | Nil)'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in '__crystal_main'
  from /opt/homebrew/Cellar/crystal/1.7.0/libexec/crystal in 'main'

Any ideas how to solve?

I don’t think big numbers work in the interpreter yet.

The bindings for libgmp probably need some adjustment.

It works if you run file requiring big decimal with interpreter:

# test.cr
require "big/big_decimal"

debugger
puts 1

Then crystal i test.cr

1 Like

Thank you, it worked. I tried:

require "big/big_decimal"

puts(BigDecimal.new("1.1") + BigDecimal.new("2.2"))

Which printed the expected 3.3.