11

I am trying to build a DLL using MinGW for Windows. I know that by default building using MinGW introduces a dependency on msvcrt.dll that ships with Windows. However, I want my DLL not to have a dependency on msvcrt.dll. Instead I want my DLL's C Runtime dependency to be satisfied using the msvcr110.dll (the Visual Studio 2012 CRT).

The reason why I need to do this is because the source code I am trying to build uses some C99 features that are not available in the VC11 compiler so it has to be built using MinGW. At the same time a DLL having a dependency on msvcrt.dll is disallowed in a Windows 8 Store application (which is what I am trying to build). Instead if a DLL has a dependency on msvcr110.dll, it is allowed in Windows Store.

So my only option is to build using MinGW but still link to msvcr110.dll.

How can I achieve this?

Raman Sharma
  • 4,551
  • 4
  • 34
  • 63

1 Answers1

9

I encourage you to download and install the latest MinGW-w64 not only because it is bleeding-edge, but it also contains the import library libmsvcr110.a to link against. Then try the following:

specs.msvcr110


%rename cpp msvcrXX_cpp

%rename cc1plus msvcrXX_cc1plus

*cpp:
%(msvcrXX_cpp) -D__MSVCRT_VERSION__=0x1100 -D__USE_MINGW_ACCESS

*cc1plus:
%(msvcrXX_cc1plus) -D__MSVCRT_VERSION__=0x1100 -D__USE_MINGW_ACCESS

*libgcc:
%{mthreads:-lmingwthrd} -lmingw32 %{shared-libgcc:-lgcc_s} -lgcc -lmoldname110 -lmingwex -lmsvcr110

libmoldname110.a


As several of you rightfully noticed, there is indeed no libmoldname110.a supplied out-of-the-box (and there are good reasons for this). Nevertheless, as usual, nobody stops you from building one on your own. To do that, you'd first need to obtain <mingw-w64-source-dir>/mingw-w64-crt/lib64/moldname-msvcrt.def, and then utilize (the sweet) dlltool as follows:

$ dlltool -k -U --as=as --def=moldname-msvcrt.def --dllname=msvcr110.dll --output-lib=libmoldname110.a

NOTE:
Unfortunately, at the moment, I don't have a chance to test this exactly myself. Hence, please, report your experience in comments so that we can jointly come up with a final solution.

test.rc


#include <winuser.h>

// Choose:
1 RT_MANIFEST msvcr110.manifest // if linking executable
2 RT_MANIFEST msvcr110.manifest // if linking DLL

msvcr110.manifest


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
      </requestedPrivileges>
    </security>
  </trustInfo>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.VC110.CRT" version="11.0.51106.1" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b">
        <file name="msvcr110.dll" />
        <file name="msvcp110.dll" />
        <file name="msvcm110.dll" />
      </assemblyIdentity>
    </dependentAssembly>
  </dependency>
</assembly>

Look at Application Manifests for more information.

Build


$ windres -i test.rc -o test.rc.o --output-format=coff
$ gcc -specs=specs.msvcr110 -o test test.c test.rc.o

Recommendation


Although Microsoft Visual C Runtime is included on most platforms, there are many different versions of it, some of which are buggy or/and break backward compatibility. Thus, it is always a good idea to distribute the version of msvcr*.dll that you know works for sure with your application.

Alexander Shukaev
  • 16,674
  • 8
  • 70
  • 85
  • 2
    There is one problem with your answer: the `libmoldname110.a` isn't included in MinGW-w64 builds – Ignitor May 17 '13 at 10:06
  • 1
    @Ignitor: You wouldn't happen to know if you have to build that lib yourself or if it is not included because it is not necessary? – Joe Jul 16 '15 at 08:48
  • 1
    It is necessary and I don't know how to build that lib. :-/ – Ignitor Jul 16 '15 at 09:44
  • Great answer, but unfortunately libmoldname110.a isn't included with MinGW-w64. Do you know how to build it? – 2501 Feb 01 '16 at 18:37
  • @2501, if this is still relevant, see the updated answer. Please, let me know whether it works for you or not. If it does not, then we'll dig further. – Alexander Shukaev Feb 01 '16 at 19:55
  • @Joe, if this is still relevant, see the updated answer. Please, let me know whether it works for you or not. If it does not, then we'll dig further. – Alexander Shukaev Feb 01 '16 at 19:55
  • @Ignitor, if this is still relevant, see the updated answer. Please, let me know whether it works for you or not. If it does not, then we'll dig further. – Alexander Shukaev Feb 01 '16 at 19:55
  • It makes the file libmoldname110.a, and it compiles the test.c, but running it crashes and gives: *The application has failed to start because its side-by-side configuration is in correct. Please see the application event log or use the command-line sxstrace.e xe tool for more detail.* (I have msvcr110.dll in the folder.) – 2501 Feb 01 '16 at 21:49
  • @2501, Check the resulting executable with **[Dependency Walker](http://www.dependencywalker.com/)** by looking at all of its dependencies and make sure that all of them are either 32-bit (if the executable itself is 32-bit) or 64-bit (if the executable itself is 64-bit). I'm just curious whether this could be the problem with some compilation flags. – Alexander Shukaev Feb 01 '16 at 21:54
  • I think they are. In the dependancy tree, MSVCR110.DLL is almost empty compared to a version with MSVCRT.DLL. (I'm not that familiar with this.) – 2501 Feb 01 '16 at 21:58
  • Hey, did you by any chance investigate this? – 2501 Feb 02 '16 at 20:38
  • Sorry, I'm quite short on time these days due to extensive work schedule. I'll try to find some time to look into this. Though, I would also recommend you to investigate this too as this is what you seem to need in the first place. Don't worry if you don't grasp much in the beginning. The understanding of tools, principles, and basic compiler infrastructure that you will obtain in the end will be invaluable. That is you have already noticed that not many people possess in-depth knowledge in that area anyway, what makes it a critical skill to have in certain situations. – Alexander Shukaev Feb 02 '16 at 21:26
  • By the way, I'd also highly recommend to knock on the MinGW-w64 mailing list regarding this issue. These people are the masters there, and if we get some good answers from them, then we could compile a nice guide here. – Alexander Shukaev Feb 02 '16 at 21:27