7

I'm trying to compile the NIF Test from Erlang (http://www.erlang.org/doc/man/erl_nif.html) on Mac OS X Lion. I can't get it to compile. Am I missing a compiler flag? Here's the error I get:

Computer:~ me $ gcc -fPIC -shared -o niftest.so niftest.c -I /usr/local/Cellar/erlang/R14B02/lib/erlang/usr/include/
Undefined symbols for architecture x86_64:
  "_enif_make_string", referenced from:
      _hello in ccXfh0oG.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

I've also tried this with -m32 but it says there's no i386 architecture either.

Thanks!

Hynek -Pichi- Vychodil
  • 26,174
  • 5
  • 52
  • 73
batman
  • 1,447
  • 5
  • 16
  • 27

3 Answers3

13

for 64-bit Erlang, the following works for me:

gcc -undefined dynamic_lookup -dynamiclib niftest.c -o niftest.so \
    -I /usr/local/Cellar/erlang/R14B02/lib/erlang/usr/include
legoscia
  • 39,593
  • 22
  • 116
  • 167
butter71
  • 2,703
  • 19
  • 12
2

It seems like your problem is not architecture but undefined symbol _enif_make_string, which means that you have to link with your enif library, whatever it is, using -l option. Also, it's been a long time since I built a shared library for OS X, but I think that the right flag to use is -dynamiclib and not -shared, and you don't have to have a space after -I.

  • If I have the `#include` directive and the -I flag, shouldn't that be enough to reference the include file? Check out the link. Thanks! – batman Nov 27 '11 at 19:54
  • 1
    @TravisPowell: No, `#include` is enough only for compiling but not for linking. Your bug is in linking, see `ld: sym...`. It is linker bug. – Hynek -Pichi- Vychodil Nov 27 '11 at 22:47
1

Try using these flags when compiling your nif instead of -shared

-bundle -flat_namespace -undefined suppress
Lukas
  • 5,182
  • 26
  • 17