how can I make nif shared libraries

Joe Armstrong <>
Wed Dec 22 20:49:15 CET 2010


How do link shared libraries with nifs?

I want to make a few nifs and put them in a shared library.
The problem is that the my nif library in its turn calls things in
other shared libraries. And when I load my nif library the other
referenced shared libraries
are not loaded.

I made a little example to illustrate this:

I have a file niftest.c with the following nif

   extern int other_lib_mult(int, int);

   static ERL_NIF_TERM mult(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
   {
      int a,b,c;
      enif_get_int(env, argv[0], &a);
      enif_get_int(env, argv[0], &b);
      c = other_lib_mult(a, b);
      return enif_make_int(env, c);
   }

I make a shared library called niftest.so
with this command

gcc -m32 -O3 -fPIC -bundle -flat_namespace -undefined suppress
-fno-common -Wall -o niftest.so niftest.c -I
/usr/local/lib/erlang/usr/include/

Which makes the shared library.

other_lib_mult is is lib.c and I make a shared library lib.so with a
similar command to the above

When I try to load niftest.so erlang says

{error,{load_failed,"Failed to load NIF library: 'dlopen(./niftest.so,
2): Symbol not found: _other_lib_mult\n  Referenced from:
/Users/joe/code/nifs/nif2/niftest.so\n  Expected in: flat namespace\n
in /Users/joe/code/nifs/nif2/niftest.so'"}}

This is running on mac os-x.

Also do all the shared libraries have to be compiled for 32 bit, or
can I pick and mix
with 64 bit code. Without the -m32 flag nothing works and some of the
shared libraries
I want to call were compiled as 64 bit.

Cheers

/Joe


More information about the erlang-questions mailing list