Skip to content

Commit 389f27f

Browse files
Fix module mode linking on macOS
When building a Lua C module (cdylib with the `module` feature) on macOS, the linker fails with undefined symbols for the Lua API. This is because macOS requires explicit `-undefined dynamic_lookup` to allow unresolved symbols in shared libraries — unlike Linux which permits them by default. The build script already had platform-specific handling for Windows (raw-dylib linking). This adds the equivalent for macOS, passing the required linker flags so that Lua symbols are resolved at load time from the host interpreter. Fixes the issue reported in #625.
1 parent 72824a4 commit 389f27f

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

mlua-sys/build/main_inner.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,22 @@ fn main() {
2626
// Check if compilation and linking is handled by external crate
2727
if cfg!(not(feature = "external")) {
2828
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
29-
if target_os == "windows" && cfg!(feature = "module") {
30-
if !std::env::var("LUA_LIB_NAME").unwrap_or_default().is_empty() {
31-
// Don't use raw-dylib linking
32-
find::probe_lua();
33-
return;
34-
}
29+
if cfg!(feature = "module") {
30+
if target_os == "windows" {
31+
if !std::env::var("LUA_LIB_NAME").unwrap_or_default().is_empty() {
32+
// Don't use raw-dylib linking
33+
find::probe_lua();
34+
return;
35+
}
3536

36-
println!("cargo:rustc-cfg=raw_dylib");
37+
println!("cargo:rustc-cfg=raw_dylib");
38+
} else if target_os == "macos" {
39+
// macOS linker requires explicit opt-in to allow undefined
40+
// symbols in dylibs. Lua C modules resolve these symbols at
41+
// load time from the host interpreter.
42+
println!("cargo:rustc-cdylib-link-arg=-undefined");
43+
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
44+
}
3745
}
3846

3947
#[cfg(not(feature = "module"))]

0 commit comments

Comments
 (0)