Skip to content

Don't ignore Debug profile on MSVC #30

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

Merged
merged 1 commit into from
May 8, 2017

Conversation

jethrogb
Copy link
Contributor

@jethrogb jethrogb commented May 7, 2017

Fixes #19

@alexcrichton Do you remember why you added this check?

I tested building cargo with this change and it seems to work fine.

@alexcrichton
Copy link
Member

Trying to remember what's going on here, my guess is that the debug profile linked with /MDd or /MTd, the debug versions of the MSVC runtimes. Rust, however, currently links with either /MD or /MT which can cause incompatibilities.

I'm pretty sure this'll break some projects, but if it works for Cargo that at least means a good number should continue to work. In that sense I'm curious to see what the breakage is here, and we can always back it out if there's too much breakage!

@alexcrichton alexcrichton merged commit d67e6ac into rust-lang:master May 8, 2017
@jethrogb
Copy link
Contributor Author

jethrogb commented May 8, 2017

Oh yeah I had forgotten what a mess Windows CRT is. Anyway, since cargo is statically linked that explains why cargo just works with this change. IIUC there would only ever be a problem when dynamically linking multiple build artifacts. In those cases however you'd want finer-grained control over what CRT is used anyway.

@lemon-sh
Copy link

Since CMake 3.15 the linked CRT can be controlled with the CMAKE_MSVC_RUNTIME_LIBRARY variable. Considering that AFAIK, Rust still uses exclusively non-debug variants of the MSVC CRT, it would be a good idea to bring back this behavior.

In fact I ran into this issue today, resulting in this linker error

error: linking with `link.exe` failed: exit code: 1120
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.44.35207\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "C:\\Users\\lemon\\AppData\\Local\\Temp\\rustc8ib3fa\\symbols.o" "<39 object files omitted>" "<sysroot>\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib/{libtest-*,libgetopts-*,libunicode_width-*,librustc_std_workspace_std-*}.rlib" "C:\\Users\\lemon\\coding\\opus-rs\\target\\debug\\deps/{libopus-40066346712875d4.rlib,libaudiopus_sys-26b7af6123061671.rlib}.rlib" "<sysroot>\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib/{libstd-*,libpanic_unwind-*,libwindows_targets-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libunwind-*,libcfg_if-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib" "kernel32.lib" "kernel32.lib" "kernel32.lib" "ntdll.lib" "userenv.lib" "ws2_32.lib" "dbghelp.lib" "/defaultlib:msvcrt" "/NXCOMPAT" "/LIBPATH:C:\\Users\\lemon\\coding\\opus-rs\\target\\debug\\build\\audiopus_sys-34690a2cb6795c2d\\out/lib" "/OUT:C:\\Users\\lemon\\coding\\opus-rs\\target\\debug\\deps\\tests-0c3c6a09f807b150.exe" "/OPT:REF,NOICF" "/DEBUG" "/PDBALTPATH:%_PDB%" "/NATVIS:<sysroot>\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:<sysroot>\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:<sysroot>\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:<sysroot>\\lib\\rustlib\\etc\\libstd.natvis"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library␍
          libaudiopus_sys-26b7af6123061671.rlib(NLSF_stabilize.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(NLSF_VQ.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(NLSF_del_dec_quant.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(LPC_inv_pred_gain.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(burg_modified_FLP.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(NLSF_VQ_weights_laroia.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(NLSF_encode.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(NSQ_del_dec_avx2.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(VAD_sse4_1.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(NSQ.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(NSQ_del_dec.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(NLSF2A.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(process_NLSFs.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(NSQ_sse4_1.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(NSQ_del_dec_sse4_1.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(energy_FLP.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(pitch_analysis_core_FLP.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(residual_energy_FLP.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(A2NLSF.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(PLC.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(CNG.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(decode_core.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(sum_sqr_shift.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(find_pred_coefs_FLP.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(process_gains_FLP.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(wrappers_FLP.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(VAD.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(resampler_private_up2_HQ.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(stereo_find_predictor.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(LP_variable_cutoff.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(noise_shape_analysis_FLP.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(opus_encoder.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(enc_API.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(stereo_LR_to_MS.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          libaudiopus_sys-26b7af6123061671.rlib(encode_indices.obj) : error LNK2001: unresolved external symbol __imp__CrtDbgReportW␍
          C:\Users\lemon\coding\opus-rs\target\debug\deps\tests-0c3c6a09f807b150.exe : fatal error LNK1120: 1 unresolved externals␍

(While trying to cargo test in https://git.catgirls.systems/lemon/opus-rs, commit 454ee65ae9, latest as of writing this)

Interestingly, setting cmake_minimum_required to 3.1 fixes the issue, so I assume some CMake behavior must have changed in a later version?

@ChrisDenton
Copy link
Member

Currently you need to pass /nodefaultlib:msvcrt to the linker.

The difficulty is that rustc itself only supports using msvcrt or libcm (when static linking). So if you want to use a debug CRT it's necessary to tell the linker to ignore what rust says.

@lemon-sh
Copy link

lemon-sh commented Jun 13, 2025

So if you want to use a debug CRT it's necessary to tell the linker to ignore what rust says.

Well, the thing is that I do not want to use the debug CRT. This is CMake configuring the project to link against it.

[UPDATE]
After digging a bit further down, it looks like since CMake 3.15 (CMP0091), specifying the CRT variant in compiler flags is no longer effective, and the aforementioned CMAKE_MSVC_RUNTIME_LIBRARY variable takes priority and must be used instead.

So from what I understand, what cmake-rs should do is set the variable to either MultiThreadedDLL or MultiThreaded, according to whether crt_static is enabled.

For reference, the workaround I'm using right now is:

let build_dir = cmake::Config::new(path)
	.define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreadedDLL")
	.build();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Why can’t we have debug info on MSVC?
4 participants