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

Alexey Romanov alexey.v.romanov@REDACTED
Wed Dec 22 21:00:51 CET 2010


Obvious question: which directory is lib.so in?

On Wed, Dec 22, 2010 at 10:49 PM, Joe Armstrong <erlang@REDACTED> 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:erlang-questions-unsubscribe@REDACTED
>
>



-- 
Yours, Alexey Romanov


More information about the erlang-questions mailing list