scoop

scoop

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

Dear community :waving_hand: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 :flexed_biceps: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

Marked As Solved

scoop

scoop

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.

Also Liked

ityonemo

ityonemo

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

odix67

odix67

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.

odix67

odix67

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

Where Next?

Popular in Questions Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one ("skipping" all the other ones)? Would mix ecto.rollback -v 2008090...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41454 115
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement