Where is erl_nif.h header file required for NIF?

HI
Please help me.

I am trying to write C code of NIF referring to the following article.

However, an error occurs because the header file cannot be found.
gcc nif.c
nif.c:1:10: fatal error: erl_nif.h: No such file or directory
#include “erl_nif.h”
^~~~~~~~~~~
compilation terminated.

Where is erl_nif.h header file required for NIF ?
Please tell me what to do.

My environment is Windows subsystem for LInux, Ubuntu 18.04.

1 Like

Depending on how you installed erlang, you might need to install additional packages.

If you installed erlang and elixir through apt (but not from ESL repos), it might be that sudo apt install erlang-dev works.

If though you installed by any other means, then you will need to check how you install the erlang headers via that package/version manager.

2 Likes

Thank you for your quick advice.
I’ll try.

It went well.
Thank you very much.

hello , i ma having same problem on windows.

Please how did you eventually resolve this?

I implement it on Linux. I have not checked on Windows. But the following code may be fine.

I referred to Matrex makefile code.

ERL_INCLUDE_PATH = $(shell erl -eval ‘io:format("~s", [lists:concat([code:root_dir(), “/erts-”, erlang:system_info(version), “/include”])])’ -s init stop -noshell)

nifs: ./lib/nifs.cu
@mkdir -p priv
nvcc -shared --compiler-options ‘-fPIC’ -lcublas -I$(ERL_INCLUDE_PATH) -o ./priv/nifs.so ./lib/nifs.cu

That code gives the PATH to the Nif header.

1 Like

I found that it could be simpler to run elixir --eval ":code.root_dir()"

Sorry to necrobump, but I’m struggling to get alex to compile as a dependency of a project with the error message:

fatal error: erl_nif.h: No such file or directory
30 | #include <erl_nif.h>
| ^~~~~~~~~~~

But the output of fd erl_nif.h / is

/usr/lib/erlang/erts-14.0.1/include/erl_nif.h
/usr/lib/erlang/usr/include/erl_nif.h

I tried setting the C_INCLUDE_PATH environment variable:

export C_INCLUDE_PATH=/usr/lib/erlang/usr/include

and tested that CPP would use it with cpp -v /dev/null -o /dev/null returning

#include <…> search starts here:
/usr/lib/erlang/usr/include
/usr/lib/gcc/x86_64-pc-linux-gnu/13.1.1/include
/usr/local/include
/usr/lib/gcc/x86_64-pc-linux-gnu/13.1.1/include-fixed
/usr/include
End of search list.

I also tried setting CMAKE_INCLUDE_PATH env variable with no joy.

Then attempting to recompile after cleaning and getting the deps again the same error is given. Same result after trying to alias cpp to include the path:

alias cpp=“cpp -I/usr/lib/erlang/usr/include”

How can I get cmake to actually find the header in this location? FWIW I’m on Arch and installed erlang and elixir with pacman.

EDIT with sort-of-solution:
Well, I was able to get it to compile by manually editing deps/alex/src/nifpp.h after mix deps.get but before mix deps.compile. Changing the line #include <erl_nif.h> to
#include </usr/lib/erlang/usr/include/erl_nif.h> seemed to do the trick. I would still like to know how to implement a more global solution than manually editing the paths every time.

EDIT2 with better sort-of-solution:
Answer to this issue on github that works for me. I’m not sure why that works but my alias attempt failed. Are there any downsides to having CXXFLAGS set to include the erlang include path even if it’s not needed?