# macOS compilation and libiconv support

**URL:** <https://forum.crystal-lang.org/t/macos-compilation-and-libiconv-support/4439>\
**Category:** Crystal Contrib\
**Created:** [March 2, 2022, 4:57pm UTC](https://forum.crystal-lang.org/t/macos-compilation-and-libiconv-support/4439 "2022-03-02T16:57:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/luislavena/32/572_2.png) [@luislavena](https://forum.crystal-lang.org/u/luislavena)\
**Post date:** [March 2, 2022, 4:57pm UTC](https://forum.crystal-lang.org/t/macos-compilation-and-libiconv-support/4439/1 "2022-03-02T16:57:25Z")

</div>

Hello folks,

(wasn’t sure if this fits here or the issue tracker, so asking as question).

Wondering if there is a particular reason why `crystal/iconv` doesn’t allow `libiconv` to be used?

The specific code: [crystal/iconv.cr at master · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/blob/master/src/crystal/iconv.cr#L1-L7)

Checks that only under win32 `lib_iconv` will be used, otherwise it falls back to whatever c/platform is defined.

For the case of macOS, that will be the bundled iconv.

You might be wondering why am I asking this question?

I’ve been trying to cross-compile to macOS from Linux. I was able to successfully produce valid executables thanks to Zig CC being used as Mach-O linker.

However, the symbols it looks for `_iconv_open`, `_iconv_close` and `_iconv` which are not part of macOS _libc_ (if we can call it that way)

Wondering if is ok to adjust the macro to something like `flag?(:use_libiconv) || flag?(:win32)`?

Here are some relevant threads about this on Zig itself:

- [cross compile to x86\_64-macos get error: "ld64.lld: error: Unable to find library for -liconv" · Issue #9725 · ziglang/zig · GitHub](https://github.com/ziglang/zig/issues/9725)
- [use case: cross-compiling to macOS with a dependency on iconv · Issue #10485 · ziglang/zig · GitHub](https://github.com/ziglang/zig/issues/10485#issuecomment-1013723542)

Any comments on why this could be a bad idea, before I submit a PR with support for this? 😁

Thank you in advance for your time reading all this!  
❤ ❤ ❤

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [March 2, 2022, 10:22pm UTC](https://forum.crystal-lang.org/t/macos-compilation-and-libiconv-support/4439/2 "2022-03-02T22:22:51Z")

</div>

Sounds like a good idea 👍

Usually we would do such things with linker configuration, but here we actually need to target different symbols. Thus it needs to be a compile time flag.

Note that there is already the `without_iconv` flag. I suppose that should perhaps be more correctly be named `without_libiconv`.

---

<div class="post-metadata">

**Author:** ![luislavena](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/luislavena/32/572_2.png) [@luislavena](https://forum.crystal-lang.org/u/luislavena)\
**Post date:** [March 3, 2022, 1:31pm UTC](https://forum.crystal-lang.org/t/macos-compilation-and-libiconv-support/4439/3 "2022-03-03T13:31:12Z")

</div>

Thank you for your quick response!

Thought about that, but considering `libiconv` is just one implementation of `iconv`, perhaps will make it `use_libiconv` as option to force it’s usage.

Will send a PR shortly and let others comment there.

Thank you again for your time!  
❤ ❤ ❤

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [March 4, 2022, 9:50am UTC](https://forum.crystal-lang.org/t/macos-compilation-and-libiconv-support/4439/4 "2022-03-04T09:50:21Z")

</div>

> [@luislavena](#):
>
> Thought about that, but considering `libiconv` is just one implementation of `iconv` , perhaps will make it `use_libiconv` as option to force it’s usage.

Yes I meant that as well. Sorry if that wasn’t clear. I just mentioned there is already a `without_iconv` flag which avoids linking against `libiconv` - but it also avoids linking against libc’s iconv. The latter should not be an issue on supported platforms. So we could consider changing the existing flag to mean `without_libiconv`. But that’s of course a separate issue and shouldn’t interfere with the action to add the `use_libiconv` flag.
