Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2015: Compile C/C++ without a runtime library

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.

like image 892
SeeSoftware Avatar asked Aug 30 '16 00:08

SeeSoftware


People also ask

What is C runtime library?

The C runtime Library (CRT) is the part of the C++ Standard Library that incorporates the ISO C standard library. The Visual C++ libraries that implement the CRT support native code development, and both mixed native and managed code. All versions of the CRT support multi-threaded development.

How do I run a CPP file without Visual Studio?

You can use devenv.exe. Use cl.exe directly (this is the c/c++ compiler and linker. Microsoft offer a make tool (similar to the unix one) called NMake.

What is UCRT and Msvcrt?

MSVCRT vs UCRT These are two variants of the C standard library on Microsoft Windows. MSVCRT (Microsoft Visual C++ Runtime) is available by default on all Microsoft Windows versions, but due to backwards compatibility issues is stuck in the past, not C99 compatible and is missing some features.

Can you compile code in Visual Studio?

Open your C++ code file in Text Editor, then use shortcut Ctrl+Alt+N , or press F1 and then select/type Run Code , or right click the Text Editor and then click Run Code in context menu, the code will be compiled and run, and the output will be shown in the Output Window.


1 Answers

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.

like image 97
Nikita Avatar answered Oct 24 '22 03:10

Nikita