-
Notifications
You must be signed in to change notification settings - Fork 460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix linking issue due to missing crt flags on windows msvc targets compiled by clang (not clang-cl). #811
Conversation
The msvc runtime library needs to be added for all targets compiled for the windows-msvc abi, regardless of the compiler. There are three options to compile code for windows msvc: - with the official msvc toolchain - with clang-cl, which has commandline syntax compatible with the msvc toolchain - with clang, which uses the GNU commandline syntax. In all three cases the -MT or -MD flag must be added. Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
This is incorrect. The flags in question have completely different meaning to clang, different from clang-cl and cl. Passing them to clang won't achieve the suggested goal. The current version is correct. But what is the problem? Linking with C run-time is handled by rustc, so one doesn't actually have to pass anything to clang if you compile C. C++ on the other hand would be problematic, and there is no reliable solution for it. Just use clang-cl. |
The background here is corrosion which integrates cargo into CMake. Corrosion sets the Some corrosion users prefer to use
Corrosion is a CMake module, so yes C++ is involved. If the code on the CMake side is built with |
For reference, all of the referred symbols are actually deprecated by Microsoft and users are advised to use their counterparts prefixed with
If you compile it with -c and examine the symbol table in the object file, you'll see that it will attempt to link with Moving on to clang. If you compile an .o file with This is the answer to the mystery about "missing" strdup, no oldnames at link time when you compile your object files with clang [not to be confused with clang-cl]. How come the suggested modification apparently helped you, I don't know. It has to be circumstantial. Because it can be easily demonstrated that passing -MD or -MT to clang.exe doesn't do anything that would be meaningful at link stage. [BTW, passing -MT is likely to trigger an error, immediately or later on, as it's supposed to be followed by an additional argument.] |
Just in case for reference. The referred linking errors are not related to C++ in any way. But the problem would be similar. If C++ code made references to C++ run-time, then the success at the linking stage would be dependent on linker directives being left by the compiler in object files. And clang.exe just doesn't do that... |
Given the context something like this should do it. index 91536c3..a0f2090 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1649,6 +1649,9 @@ impl Build {
// depending on the origin clang can default to a mismatchig
// run-time.
cmd.push_cc_arg(format!("--target={}", target).into());
+ if target.contains("msvc") {
+ self.print(&format_args!("cargo:rustc-link-lib=oldnames"));
+ }
}
if cmd.family == ToolFamily::Clang && target.contains("android") { Just in case, I for one would advocate rather in favour of not supporting non-cl compilers in msvc target context:-) |
Fixes problem discussed in rust-lang#811.
Fixes problem discussed in rust-lang#811. [Also deduplicate clang --target argument on Windows, because it's passed later on in the "// Target flags" section.]
Fixes problem discussed in rust-lang#811. [Also deduplicate clang --target argument on Windows, because it's passed later on in the "// Target flags" section.]
Fixes problem discussed in rust-lang#811. [Also deduplicate clang --target argument on Windows, because it's passed later on in the "// Target flags" section.]
The msvc runtime library needs to be added for all targets compiled for the windows-msvc abi, regardless of the compiler. There are three options to compile code for windows msvc:
In all three cases the -MT or -MD flag must be added. Without this commit, the option is only added in the first two cases.