[erlang-questions] how can I make nif shared libraries

Tony Rogvall <>
Wed Dec 22 21:06:51 CET 2010


Hi joe!

To compile for multiple architectures on mac-os-x I use the flags

-arch i386 -arch x86_64

instead of -m32.

This makes it possible to use the driver or nif shared object with both 32- or 64-bit erlang.

To link with other shared libraries you need to add

-L<path> -l<name>

Sometimes it gets a bit more complicated than this, but this should be enough for you?
I sometimes revert to link the the code instead of using shared libraries. Then you 
do not have to think about dependencies.

Hope it works

/Tony


On 22 dec 2010, at 20.49, Joe Armstrong wrote:

> 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
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
> 

"Have run Make so many times I dunno what's installed anymore"



More information about the erlang-questions mailing list