@@ -26,22 +26,30 @@ pub fn setup_python_linking() {
26
26
// Get the Python interpreter configuration directly
27
27
let config = pyo3_build_config:: get ( ) ;
28
28
29
- // Add the library search path if available
29
+ let is_extension_module = std:: env:: var ( "CARGO_FEATURE_EXTENSION_MODULE" ) . is_ok ( ) ;
30
+
31
+ // Only link libpython explicitly if we are NOT building an extension module.
32
+ if !is_extension_module {
33
+ if let Some ( lib_name) = & config. lib_name {
34
+ println ! ( "cargo:rustc-link-lib=dylib={}" , lib_name) ;
35
+ } else {
36
+ // Warn only if linking is actually needed but we can't find the lib name
37
+ println ! ( "cargo:warning=Python library name not found in config (needed for non-extension module
38
+ builds)." ) ;
39
+ }
40
+ }
41
+
42
+ // Add the library search path and RPATH if available.
43
+ // These are needed for test executables and potential future standalone binaries,
44
+ // and generally harmless for extension modules.
30
45
if let Some ( lib_dir) = & config. lib_dir {
31
46
println ! ( "cargo:rustc-link-search=native={}" , lib_dir) ;
32
-
33
- // Add RPATH linker argument for Unix-like systems (Linux, macOS)
34
- // This helps the test executable find the Python library at runtime.
35
47
#[ cfg( not( windows) ) ]
36
48
println ! ( "cargo:rustc-link-arg=-Wl,-rpath,{}" , lib_dir) ;
37
49
} else {
38
- println ! ( "cargo:warning=Python library directory not found in config." ) ;
39
- }
40
-
41
- // Add the library link directive if available
42
- if let Some ( lib_name) = & config. lib_name {
43
- println ! ( "cargo:rustc-link-lib=dylib={}" , lib_name) ;
44
- } else {
45
- println ! ( "cargo:warning=Python library name not found in config." ) ;
50
+ // Warn only if linking is actually needed but we can't find the lib dir
51
+ if !is_extension_module {
52
+ println ! ( "cargo:warning=Python library directory not found in config." ) ;
53
+ }
46
54
}
47
55
}
0 commit comments