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

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement