Compiling C code for use in NIF; error: unloadable mach-o file type 1

Dear community :wave:t2:

I’m trying to get NIFs off the ground!

Oh boy :joy:

Goal

My goal is call a library written in C.

The reason for this is to avoid re-implementing code translating geodetic coordinates from/to military grid reference system (for details, see MGRS on Wikipedia).

Background

This is a partly contrived side project I am doing for the primary purpose to educate myself. It is part of putting together a LiveView with a MapBox GL JS map to draw military map symbols conforming to MIL-STD-2525D using an old JS “milsymb” library from Spatial Illusions. It could be quite nifty.

Why? Because it’s an old grudge I’ve been keeping from ages ago when I served in a mechanized battalion HQ and we had the most terrible command and control systems.

There is already literally battle tested C code that I believe is best to use in this case. In particular, it’s an older version of the code produced by the US Geospatial Intelligence Agency (NGA), Office of Geomatics, called GEOTRANS 3.8. The newer version that I linked to is C++ with Java around it. For my limited purposes, I’ve elected to go with the legacy implementation included in repositories scattered around GitHub.

However, I’ve managed to get bogged down while trying to call a simple function calculating the sum of two integers :muscle:t2:

Code

Implementation of nif_test.c.

#include "erl_nif.h"

static ERL_NIF_TERM sum(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  int a, b, result;
  enif_get_int(env, argv[0], &a);
  enif_get_int(env, argv[1], &b);
  result = a + b;
  return enif_make_int(env, result);
}

static ErlNifFunc nif_funcs[] = {
  {"sum", 2, sum}
};

ERL_NIF_INIT(Elixir.NifTest, nif_funcs, NULL, NULL, NULL, NULL)

Compilation of the above C code.

clang -fPIC -o nif_test.so -c nif_test.c -I/opt/homebrew/Cellar/erlang/25.0.3/lib/erlang/erts-13.0.3/include

The compilation does not fail. It produces a nif_test.so as ordered. I believe that is where things are going sideways however.

Note the unloadable mach-o file type 1.

Implementation of nif_test.ex.

defmodule NifTest do
  @on_load :load_nifs

  def load_nifs do
    :erlang.load_nif('./nif_test', 0)
  end

  def sum(_a, _b) do
    raise "NIF sum not implemented"
  end

end

Issue

Upon compiling the Elixir code I get

iex(1)> c "nif_test.ex"

10:18:24.257 [warning] The on_load function for module Elixir.NifTest returned:
{:error,
 {:load_failed,
  'Failed to load NIF library: \'dlopen(./nif_test.so, 0x0002): tried: \'./nif_test.so\' (unloadable mach-o file type 1 \'./nif_test.so\'), \'/Users/[REDACTED]/Developer/geotrans/nif_test.so\' (unloadable mach-o file type 1 \'/Users/[REDACTED]/Developer/geotrans/nif_test.so\')\''}}

[NifTest]
iex(2)>

Now, I think the .so is kaputt.

nm nif_test.so     
                 U _enif_get_int
                 U _enif_make_int
00000000000000d8 d _nif_funcs
0000000000000000 T _nif_init
0000000000000078 d _nif_init.entry
000000000000000c t _sum
00000000000000f8 s l_.str
0000000000000107 s l_.str.1
0000000000000114 s l_.str.2
000000000000011e s l_.str.3
0000000000000000 t ltmp0
0000000000000078 d ltmp1
00000000000000f8 s ltmp2
0000000000000128 s ltmp3

Looking for only defined symbols, there is no dynamic symbol table.

nm -D nif_test.so  
/Library/Developer/CommandLineTools/usr/bin/nm: error: nif_test.so: File format has no dynamic symbol table

So, don’t I realise that I’m just faced with a C-issue that’s up to me dive into and dust off my old course literature from the university course on imperative languages?

Well, I sure do.

Honestly, I haven’t managed to fix it despite trying. I haven’t found proper resources on which flags I should send to clang. Ones such as -shared just produces warnings about these being ignored (i.e. not very helpful).

If anyone spots an obvious issue or feels like this is an issue worth while I’d be terribly thankful for any input.

Environment

macOS 12.5 Monterrey (M1)

Apple clang version 13.1.6 (clang-1316.0.21.2.5)
Target: arm64-apple-darwin21.6.0
Thread model: posix

I would propose that your compile options aren’t correct, there exists a good blog post from Andrea Leopardi Using C from Elixir with NIFs. I personally felt in love using zig with zigler to wrap external libraries, regardless if they are C or C++, for the latter, of course, I’ve to provide a C-ABI wrapper.

2 Likes

The next release of zigler will have a way to auto-import C functions from .h files :D. (I think).

3 Likes

Thank you @odix67 and @ityonemo!

I hadn’t discovered zigler so, obviously, that was an excellent tip :slight_smile:

I looked at Andrea Leopardi’s tutorial for compiling the C code but the flags are ignored by clang. Very determined to dig myself out of this C-hole and get to the fun parts.

If you want to stick with C at the moment, take a look at the proj wrapper implementation of proj4 for elixir

1 Like

Thanks @odix67! This is a really good suggestion,

I hadn’t stumbled upon that wrapper so I’m very thankful for you bringing it to my attention.

Indeed, the compiler flags were incorrect.

I’ve managed to compile the C code with the following flags.

-g -O3 -ansi -pedantic -Wall -Wextra -Wno-unused-parameter -std=gnu99 -Wno-unused-function

And in the Makefile (being on Darwin):

ifneq ($(OS),Windows_NT)
	CFLAGS += -fPIC

	ifeq ($(shell uname),Darwin)
		LDFLAGS += -dynamiclib -undefined dynamic_lookup
	endif
endif

With this I’m also able to load and execute the NIF. Now I’m going to look into each of them to get a better understanding.

Regarding the compiler flags, it was this blog post on NIFs by Meng Xuan Xia that helped me resolve this particular issue.

I’m going to tidy up my wrapper and publish it knowing full well that nobody cares about MGRS :slight_smile: It’s for the learning experience.

Really love Elixir and this community.

2 Likes