我在OSX 10.6 Snow Leopard上玩http://www.erlang.org/doc/tutorial/nif.html。我在运行这个例子时遇到了一些麻烦。
我使用编译:
gcc -o complex_nif.so -fpic -I/usr/local/lib/erlang/usr/include -flat_namespace -undefined suppress complex.c complex_nif.c
使用以下命令在erlang中运行:
Erlang R15B01 (erts-5.9.1) [source] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9.1 (abort with ^G)
1> c(complex).
{error,on_load_failure}
=ERROR REPORT==== 4-Apr-2013::15:24:51 ===
Error in process <0.37.0> with exit value: {{badmatch,{error,{load_failed,"Failed to load NIF library: 'dlopen(./complex_nif.so, 2): no suitable image found. Did find:\n ./complex_nif.so: mach-o, but wrong architecture'"}}},[{complex,init,0,[{file,"...
=ERROR REPORT==== 4-Apr-2013::15:24:51 ===
The on_load function for module complex returned {{badmatch,
{error,
{load_failed,
"Failed to load NIF library: 'dlopen(./complex_nif.so, 2): no suitable image found. Did find:\n\t./complex_nif.so: mach-o, but wrong architecture'"}}},
[{complex,init,0,
[{file,...},{...}]},
{code_server,
'-handle_on_load/4-fun-0-',
1,
[{...}|...]}]}
我看到我找到了.so文件。但是erlang不喜欢它编译的架构,erlang更喜欢哪种架构?
答案 0 :(得分:3)
验证Erlang运行时系统使用的体系结构。 E.g:
$ which erlc
/usr/local/bin/erlc
$ file /usr/local/bin/erlc
/usr/local/bin/erlc: Mach-O 64-bit executable x86_64
确保您的库符合上述架构:
$ file complex_nif.so
complex_nif.so: Mach-O 64-bit executable x86_64
如果它们不匹配,请使用-arch i386
(或-arch x86_64
)标记编译库。