Conflict Between /MT and /MD When Using Windows.winmd with Third-Party Libraries

checkingrandom 226 Reputation points
2024-10-14T12:33:02.72+00:00

Hi everyone,

I am currently working on a project using C++/CX that interacts with the Windows WinMD, specifically within the Windows.Management.Deployment namespace. I’ve encountered a compatibility issue related to the runtime libraries.

The third-party libraries I am using must be built with the /MD (Multi-threaded DLL) runtime. However, my project is configured to use /MT (Multi-threaded) runtime. When I try to build the project along with these libraries using /MT, I receive the following error:

'/ZW' and '/MT' command-line options are incompatible.

From what I understand, using the Windows Runtime API requires the /MD option. Is there a workaround that would allow me to use /MT for the third-party libraries, or is it strictly required to use /MD for any project that interacts with the Windows Runtime?

I would greatly appreciate any guidance on whether it's possible to mix these runtime types, or if there is indeed a strict requirement to use /MD when working with the Windows Runtime API.

Thanks in advance for your help!

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,442 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,743 questions
{count} votes

Accepted answer
  1. Darran Rowe 1,021 Reputation points
    2024-10-14T20:20:04.56+00:00

    I believe that there is some confusion between the Windows Runtime and C++/CX going on here. C++/CX was the original Windows Runtime projection for C++, it is not the Windows Runtime itself.

    C++/WinRT, the newer projection, works perfectly fine with the static runtime library, so using /MT or /MTd works fine. So my suggestion is to disable anything that was set for using the Windows Runtime and then open Solution Explorer. In Solution Explorer, right click on the project name or the references under the project name.

    Screenshot 2024-10-14 210646

    In this context menu, select "Manage NuGet Packages". In the window that appears, switch to browse and then search for Microsoft.Windows.CppWinRT, and install that package.

    Screenshot 2024-10-14 211224

    If the project is built once, then the generated files for the Windows Runtime can be found in the intermediate directory for the project.

    The following shows a simple C++/WinRT source file that just creates an instance of the PackageManager runtime class.

    #include <Windows.h>
     
    #undef GetCurrentTime
     
    #include <Unknwn.h>
    #include <winrt/Windows.Foundation.h>
    #include <winrt/Windows.Foundation.Collections.h>
    #include <winrt/Windows.Management.Deployment.h>
     
    #include <iostream>
     
     
    int main()
    {
        winrt::init_apartment(winrt::apartment_type::multi_threaded);
     
        auto package_manager = winrt::Windows::Management::Deployment::PackageManager{};
     
        return 0;
    }
    

    The C++/WinRT documentation can be found easily, but it is a bit UWP centric.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.