Dear community
I’m trying to get NIFs off the ground!
Oh boy
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
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