#define iconv_open libiconv_open in libiconv 1.15


I am new to crystal lang. I want to build the latest version on macOS but have some problems.

$ cd crystal-0.28.0
$ make
ar -rcs src/ext/libcrystal.a src/ext/sigfault.o
CRYSTAL_CONFIG_PATH="/Volumes/U/Users/ici/work/crystal/crystal-0.28.0/src"        CRYSTAL_CONFIG_BUILD_COMMIT="" ./bin/crystal build -D preview_overflow -D compiler_rt  -o .build/crystal src/compiler/crystal.cr -D without_openssl -D without_zlib
Undefined symbols for architecture x86_64:
  "_iconv", referenced from:


ld: symbol(s) not found for architecture x86_64

In libiconv 1.15 iconv_open is a cpp macro to libiconv_open in iconv.h and, I guess, this is the reason of the above link error of crystal-0.28.0 on macOS with macports libiconv installed. (with llvm 6.0.1)

In crystal-0.28.0, iconv_open is invoked in src/iconv.cr in the following way.

iconv = LibC.iconv_open(to, from)

Is this guess right ? Let me know whether the macro defined in iconv.h is applied or how to apply the macro for building crystal on macOS.

Thanks in advance.

There’s no way to run a C macro from Crystal. Can’t you install it using homebrew?

Thanks for the comment.

I understand that about the C macro. I found that there are two iconvs, one is in /usr/lib and the other is in /opt/local/lib. Since the link options contain -L/opt/local/lib explicitly, /opt/lib/libiconv.a is used for -liconv. If the option is changed from -liconv to /usr/lib/libiconv.a, the link error may not happen.

Let me know how -L/opt/local/lib is acquired in make procedure ? I want to try.

Where? I think we don’t pass link options when we compile Crystal. It just gets the default ones (I can’t remember how that works).

I found how to build crystal on macOS. The answer is found in what macports or homebrew does.

Basically, it looks src/lib_c/x86_64-macosx-darwin/c/iconv.cr is not good when GNU libiconv, in which iconv_open is a cpp macro, is used. What they do is to replace the file with amd64-unknown-openbsd/c/iconv.cr.

Now, make of crystal-0.28.0 has successfully finished.

Thank you.