scoop

scoop

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

Showing Posts 1 to 6

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.

ityonemo

ityonemo

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

scoop

scoop OP

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.

odix67

odix67

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

scoop

scoop OP

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.

scoop

scoop OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews