What to do when /MT and /MD both required?

2019-05-16 08:52发布

问题:

This is a sort of follow-up to this question I posted yesterday. My problem regards which runtime C++ libraries to link against. I am using Qt as a framework and QtCreator for my IDE. According to the digia docs here, Qt has been known to have memory problems when built with the /MT flag (which makes your app run against the static runtime libraries). However, I'm also using a 3rd party driver in this app, and the docs on that app specifically say that it won't build unless you link against the static runtime libraries. Sure enough, it compiles fine with the /MT flag, but gives me about 40 linker errors when I remove that setting. (and so far I'm only including one header file from the driver's static library)

So my question is: what's the correct thing to do here? Is there a way to force the driver to expect the dynamic runtime library? Or should I live with Qt's memory management problem? Or is there a way to have Qt link against the dynamic ones and the driver (and the parts of Boost that it requires) link against the static ones? (and keep in mind that I'm doing this in QtCreator, not studio)

回答1:

Both /MT and /MD are linker options. If you're building multiple modules, you can have multiple options.

In this case, use /MD for Qt and your own code. Wrap the driver in its own DLL, with a non-CRT-dependent API, and link that DLL with /MT. Using COM might be an option. That's certainly not CRT dependent, but it may be overkill.