You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac M1上Erlang NIF链接失败求助(基于erlang.mk)

在Mac M1上使用erlang.mk链接Erlang NIF与Sunvox动态库的编译问题

我在Mac M1设备上通过kerl安装了Erlang,尝试用erlang.mk将Erlang NIF与Sunvox(一款小型软件合成器)的动态库(dylib)链接时遇到编译错误,已确认使用的是ARM版本的dylib而非x86版本。

c_src目录结构

jhw@my-MacBook-Air myapp % ls -l -R c_src         
total 16
drwxr-xr-x  4 jhw  staff  128 26 Aug 16:26 sunvox
-rw-r--r--  1 jhw  staff  782 26 Aug 17:04 sunvox_nif.c

c_src/sunvox:
total 0
drwxr-xr-x  3 jhw  staff  96 26 Aug 17:15 include
drwxr-xr-x  3 jhw  staff  96 26 Aug 16:35 lib

c_src/sunvox/include:
total 104
-rw-r--r--@ 1 jhw  staff  49805 26 Aug 16:31 sunvox.h

c_src/sunvox/lib:
total 1504
-rwxr-xr-x@ 1 jhw  staff  767321 26 Aug 16:35 libsunvox.dylib

sunvox_nif.c代码

#include "erl_nif.h"
#include "sunvox.h"

static ERL_NIF_TERM nif_sv_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
    int res = sv_init(0, 44100, 2048, 0);
    if (res != 0) {
      return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_int(env, res));
    }    
    return enif_make_atom(env, "ok");
}

static ERL_NIF_TERM nif_sv_deinit(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
    int res = sv_deinit();
    if (res != 0) {
      return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_int(env, res));
    }    
    return enif_make_atom(env, "ok");
}
    
static ErlNifFunc nif_funcs[] = {
    {"sv_init", 0, nif_sv_init},
    {"sv_deinit", 0, nif_sv_deinit}
};

ERL_NIF_INIT(sunvox_nif, nif_funcs, NULL, NULL, NULL, NULL)

Makefile配置

PROJECT = myapp
PROJECT_DESCRIPTION = An Erlang server to wrap the Sunvox developer library as a simple DJing service
PROJECT_VERSION = 0.1.0

CFLAGS += -I$(CURDIR)/c_src/sunvox/include

LDFLAGS += -L$(CURDIR)/c_src/sunvox/lib -lsunvox

C_SRC_TYPE = shared
C_SRC_DIR = $(CURDIR)/c_src
C_SRC_OUTPUT = $(CURDIR)/priv/$(PROJECT)

BUILD_DEPS += relx
include erlang.mk

DYLD_LIBRARY_PATH设置

#!/usr/bin/env bash

export DYLD_LIBRARY_PATH=$HOME/work/myapp/c_src/sunvox/lib

编译错误信息

执行make clean && make时出现以下错误:

jhw@my-MacBook-Air myapp % make clean && make
erlang.mk:26: Please upgrade to GNU Make 4 or later: https://erlang.mk/guide/installation.html
 GEN    clean-app
 GEN    clean-c_src
 GEN    coverdata-clean
erlang.mk:26: Please upgrade to GNU Make 4 or later: https://erlang.mk/guide/installation.html
 DEPEND myapp.d
 ERLC   myapp_app.erl myapp_sunvox_server.erl myapp_sup.erl
 APP    myapp
 C      sunvox_nif.c
 LD     myapp.so
Undefined symbols for architecture arm64:
  "_enif_make_atom", referenced from:
      _nif_sv_init in sunvox_nif.o
      _nif_sv_deinit in sunvox_nif.o
  "_enif_make_int", referenced from:
      _nif_sv_init in sunvox_nif.o
      _nif_sv_deinit in sunvox_nif.o
  "_enif_make_tuple", referenced from:
      _enif_make_tuple2 in sunvox_nif.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/Users/jhw/work/myapp/priv/myapp.so] Error 1

erlang.mk作者表示该库的链接指令在M1上运行异常,我并非C语言专家,恳请告知如何修改Makefile以完成NIF的正常编译。

内容的提问来源于stack exchange,提问作者Justin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 19:52:44