3

Visual Studio 2012 (and earlier versions) are capable of compiling C code. Plain C, not C++. It would be a good feature if you wanted to avoid the runtime hazzle. I thought of compiling plain C binaries and was hoping to do so without the MSVCRT runtime.

After adding the /TC (compile as C) option I was hoping to get a binary with only basic dependencies such as kernel32 and ntdll. But instead, this was linked:

enter image description here

We want to use VS 2012 and not the runtime. The GCC compiler doesn't need it, so there must be a way to compile a "simple" binary in VS, too. We don't necessarily need complex string functions or date/time libraries, just simple code.

Question: Is it possible to compile C code in Visual Studio 2012 without the MSVCRT runtime (or even C++ code) ?

Edit: without static linking (/MT)

bytecode77
  • 12,331
  • 27
  • 101
  • 126
  • I'm not too familiar with VS/Windows, but where would things like `printf`, `strcmp`, `memcpy` reside? It was my impression that the MSVC runtime is kind of like what `libc.so` is on linux. – us2012 Sep 28 '13 at 22:37
  • Why not `/MT`? I think it links only stuff you actually use? So if you don't use anything from MSVCRT it wont link anything. – user2802841 Sep 28 '13 at 22:42
  • I'm not sure, but I think `memcpy` is also available in a different DLL, way more distributed over windows computers. – bytecode77 Sep 28 '13 at 22:44
  • I would use `msvcrt.dll` for this. Is that possible? – bytecode77 Sep 28 '13 at 22:55
  • 1
    http://www.drdobbs.com/avoiding-the-visual-c-runtime-library/184416623 has some information on how to achieve runtime independence. – namezero Sep 28 '13 at 23:49

1 Answers1

6

The correct answer to the question "Is it possible to compile C code in Visual Studio 20xx without the MSVCRT runtime (or even C++ code)?" is to use the /MT option (Configuration Properties > C/C++ > Code Generation > Runtime Library=Multi-threaded (/MT)). This creates an executable with no dependencies on any MSVCRTxx exactly as you wanted. As far as I know, that's all it does. It places no restrictions on anything you want to do - all the standard C library functions like memcpy still work. The only other difference is that the .EXE file is slightly larger. I've been making and distributing EXE files created like this from pure ANSI C code for years without any problems whatsoever using MSVC6, MSVC2005, MSVC2008 and MSVC2013.

As to the answer to the question with the qualifier "without static linking (/MT)", well, you can't.

David I
  • 787
  • 4
  • 9
  • Not quite all true. You can compile a DLL with no runtime and no static linking right from VS2005 to VS2015. The caveats are that you are cautious about undocumented behaviour, avoiding unmanaged C++ and sticking to plain unmanaged C. Global statics do not get initialised and you may need to create your own micro-runtime sticking to user32 and kernel32 API calls. This is especially important if making a DLL plugin for a product compiled with a different runtime to the platform you are compiling with. It is possible to create C++/CLI .Net plugin DLLs with a dependency only on kernel32.dll – birdwes Oct 26 '17 at 21:25