-
Notifications
You must be signed in to change notification settings - Fork 44
Description
Since Rust 1.61 1, the linker no longer includes the static libraries with the --whole-archive
flag. This means that the resource.o
file is only added to the final EXE if it defines any symbol undefined elsewhere. But resource objects do not define any symbol at all!
There are several solutions for this that I can think of:
Solution n.1: The fancy one.
- println!("cargo:rustc-link-lib=static=resource");
+ println!("cargo:rustc-link-lib=static:+whole-archive=resource");
This forces the --whole-archive
flag for this particular library, restoring the old behavior. The drawback is that I think it will not be backwards compatible, it will require Rust 1.61.
Solution n.2: The classic.
- println!("cargo:rustc-link-lib=static=resource");
+ println!("cargo:rustc-link-args={}", output.display()); // .../resource.o
By passing the name of the object file instead of the library it will link the given object into the EXE, unconditionally. The nice thing is that it is backwards compatible. And the call to AR is not longer needed.
Solution #3: The hack.
- let output = PathBuf::from(output_dir).join("resource.o");
+ let output = PathBuf::from(output_dir).join("libresource.a");
And then removing the call to AR. Then when linking the static library resource
the linker will find the libresource.a
, that is not an archive but an object, thus it is included unconditionally. It is similar to n.2 but it still uses the static
flag.