MickeyOoh

MickeyOoh

Undefined error of "enif_make_string" happens to erlang nif function

I am checking Nif function following Erlang erl_nif document and having an issue of compile error.
Could you please help me to solve this issue?

/* niftest.c */
#include <erl_nif.h>
static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
      { return enif_make_string(env, "Hello world!", ERL_NIF_LATIN1); }
static ErlNifFunc nif_funcs[] = { {"hello", 0, hello} };
ERL_NIF_INIT(niftest,nif_funcs,NULL,NULL,NULL,NULL)

compiling

$> gcc -fPIC -shared -o niftest.so niftest.c -I $ERL_ROOT/usr/include/
Undefined symbols for architecture x86_64:
 “_enif_make_string”, referenced from:
   _hello in niftest-cb9456.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

machine: macOS Monetery
gcc: Apple clang version 13.1.6 (clang-1316.0.21.2)
erlang: 24.3
include file: ERL_ROOT=~/.asdf/installs/erlang/24.3

It is where undefined symbol “enif_make_string” is defined.
erl_nif_api_funcs.h

#ifdef ERL_NIF_API_FUNC_DECL
  ERL_NIF_API_FUNC_DECL(
      ERL_NIF_TERM,
      enif_make_string,
      (ErlNifEnv* env, const char* string, ErlNifCharEncoding)
  );
#endif

#ifdef ERL_NIF_API_FUNC_MACRO
    #define enif_make_string ERL_NIF_API_FUNC_MACRO(enif_make_string)
#endif

The following is where two switching Macros is defined.
ERL_NIF_API_FUNC_DECL, ERL_NIF_API_FUNC_MACRO
erl_nif.h

#if (defined(__WIN32__) || defined(_WIN32) || defined(_WIN32_))
  #define ERL_NIF_API_FUNC_DECL(RET_TYPE, NAME, ARGS) RET_TYPE (*NAME) ARGS typedef struct {
    #include "erl_nif_api_funcs.h"
    void* erts_alc_test;
  } TWinDynNifCallbacks;
  extern TWinDynNifCallbacks WinDynNifCallbacks;
  #undef ERL_NIF_API_FUNC_DECL
#endif
#if (defined(__WIN32__) || defined(_WIN32) || defined(_WIN32_)) 
        && !defined(STATIC_ERLANG_DRIVER) && !defined(STATIC_ERLANG_NIF)
  #define ERL_NIF_API_FUNC_MACRO(NAME) (WinDynNifCallbacks.NAME)
  #include "erl_nif_api_funcs.h"
  /* note that we have to keep ERL_NIF_API_FUNC_MACRO defined */
#else /* non windows or included from emulator itself */
  #define ERL_NIF_API_FUNC_DECL(RET_TYPE, NAME, ARGS) extern RET_TYPE NAME ARGS
  #include "erl_nif_api_funcs.h"
  #undef ERL_NIF_API_FUNC_DECL
#endif

I couldn’t find switching macros of “__WIN32__” anywhere although this looks WindowsOS macro.

This issue happened on Erlang 23.x and I installed 24.3. So installation doesn’t cause this.

Please help me to find what I am missing.
Thanks.

Marked As Solved

garazdawi

garazdawi

Erlang Core Team

After checking again I think the -bundle is supposed to replace the -shared for clang. Does that work for you?

Also Liked

garazdawi

garazdawi

Erlang Core Team

On MacOS you also need to pass -bundle -bundle_loader $ERL_ROOT/erts-*/bin/beam.smp to gcc/clang in order for it to work.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement