[erlang-questions] NIF Query

Steve Vinoski vinoski@REDACTED
Sat Sep 2 15:24:45 CEST 2017


On Fri, Sep 1, 2017 at 7:13 PM, John Duffy <jb_duffy@REDACTED> wrote:

> Hi
>
> I'm trying to follow the Interoperability Tutorial Users Guide (Version
> 9.0) to create a module containing some simple NIFS. I have managed to do
> this, however I have needed to digress from the documentation as follows...
>
> 1.)  if my module name doesn't match the filename in the call to
> erlang:load_nif("filename", 0) I get a compile error. This is not as per
> the example in the documentation, i.e. -module(complex6) &
> erlang:load_nif("complex6_nif, 0).
>

Can you supply the details of the error you're getting? I followed the
tutorial on my Mac and had no problems.

2.)  The appears to be a "flags" parameter missing in the {"foo", 1,
> foo_nif} structure in the documentation which generates a compile error...
>
> static ErlNifFunc nif_funcs[] = {
>
>     {"foo", 1, foo_nif},
>     {"bar", 1, bar_nif}
> };
>
>
> I have changed this to that below, which compiles, but what is the nature/value of "flags"?
>
>
> static ErlNifFunc nif_funcs[] = {
>
>     {"foo", 1, foo_nif, 0},
>     {"bar", 1, bar_nif, 0}
> };
>
>
>
> I may be missing something, or the Tutorial may need updating?
>

The flags field is described in http://erlang.org/doc/man/erl_nif.html --
it's currently used for values specific to dirty NIFs, which are also
described on that page. For a regular NIF like the one in the tutorial,
flags should be 0. Since the nif_funcs variable in the tutorial is defined
as static, the C compiler is guaranteed to initialize flags to 0 because an
initial value for it isn't specified.

Whether you get a problem from the flags field or not depends on your
compiler and the options you pass to it. For example, copying and pasting
the complex6_nif.c from the tutorial into a file and compiling it as
follows on my Mac produced no errors or warnings:

clang -o complex6_nif.so complex.c complex6_nif.c
-I/usr/local/lib/erlang/usr/include -bundle -flat_namespace -undefined
suppress

But adding the -Wmissing-field-initializers option results in warnings:

complex6_nif.c:27:23: warning: missing field 'flags' initializer
[-Wmissing-field-initializers]
    {"foo", 1, foo_nif},
                      ^
complex6_nif.c:28:23: warning: missing field 'flags' initializer
[-Wmissing-field-initializers]
    {"bar", 1, bar_nif}
                      ^
2 warnings generated.

--steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170902/1a5f16d0/attachment.htm>


More information about the erlang-questions mailing list