20

Is there a way of compiling C/C++ with Visual Studio 2015 without using any runtime library?

I need to compile without a runtime library because I'm creating my own runtime library (for my OS).

There are options on C/C++->Code Generation->Runtime Library
but I want an option that says "none".

I'm aware of loosing a lot of features that are in the CRT.

Steve
  • 6,334
  • 4
  • 39
  • 67
SeeSoftware
  • 531
  • 6
  • 17
  • 2
    Last I heard, it was not possible to compile an OS kernel with Visual Studio - it's simply not designed for it, and many parts of it assume you're running on WIndows. – user253751 Aug 30 '16 at 00:45
  • 2
    Also related: Visual Studio produces executables in the PE file format. What file format does your OS run? – Mooing Duck Aug 30 '16 at 00:47
  • 3
    Also: start here: http://wiki.osdev.org/Visual_Studio and proceed directly to "Custom C++ Runtime" – Mooing Duck Aug 30 '16 at 00:48
  • 3
    It is technically possible, as long as (a) the code doesn't make any CRT or Win32 calls, and (b) you override the default entry point (_DllMainCRTStartup or _WinMainCRTStartup) which pulls in the CRT by default. As commented already, however, you'll still end up with a binary in PE format using the Win32 ABI. – dxiv Aug 30 '16 at 00:51
  • 2
    @dxiv: Though there is a pretty decent chance that PE is what's desired, since that's (at least sort of) what UEFI supports/uses. – Jerry Coffin Aug 30 '16 at 01:02
  • 1
    This seems like a pretty specific question about a programming tool. I cleaned up the question. Not sure why it's downvoted. – Steve Aug 30 '16 at 01:08
  • One way to find more info: `link /? | find /i "lib"`. Another way is to search for questions about smallest executable for Windows and Unixland. – Cheers and hth. - Alf Aug 30 '16 at 02:00
  • 1
    Related forum post with useful information on this topic: [Handmade Hero forums: Guide - How to avoid C/C++ runtime on Windows](https://hero.handmade.network/forums/code-discussion/t/94-guide_-_how_to_avoid_c_c++_runtime_on_windows) – jrh Feb 12 '19 at 20:35

1 Answers1

19

To compile your app without C-Runtime Library (CRT) use /MT, /NODEFAULTLIB linker options and redefine entry point at Linker -> Advanced -> Entry Point to function defined in your code, e.g. rawMain. The signature is:

DWORD CALLBACK rawMain();

Without C-runtime library you are not allowed to use it's functions, like malloc, free, memset, etc. You should implement all the used CRT functions by yourself. E.g. you can replace usage of malloc by VirtualAlloc() and free by VirtualFree().

To check that C-runtime is not linked to your application use Dependency Walker.

Nikita
  • 6,270
  • 2
  • 24
  • 37
  • Hint for further investigation, CRT implementation of the main function is located in `VC\crt\src\vcruntime\exe_common.inl` (VS2015) – Nikita Aug 30 '16 at 07:30
  • @SeeSoftware Make sure `Linker -> Input -> Ignore All Default Libraries` is set to `Yes` and check that `Linker -> Input -> Additional Dependencies` list doesn't contain any CRT libraries. Also make sure you don't include any [`C++ Standard Library Header Files`](https://msdn.microsoft.com/en-us/library/a7tkse1h.aspx) because in this case Standard C++ Library will be linked in automatically. – Nikita Aug 30 '16 at 12:31
  • i had the correct settings but forgott to change debug to release – SeeSoftware Aug 30 '16 at 16:11
  • In my case we used IDA and wanted to remove all the `_scrt` functions. We did it like describe: `Linker -> Advanced -> Entry Point` and we had "main" function so added it as "main" and now it removed all these functions. – E235 Jul 28 '22 at 16:23