Page 1 of 1

Microsoft : Writing C Applications Without The Runtime System Rate Topic: -----

#1 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Posted 22 April 2011 - 12:43 AM

Microsoft : Writing C Applications Without The Runtime System

Introduction

I must emphasize from the offset, that this tutorial is most definitively not for beginners, as it describes how to write C applications without the C runtime system. Such routines such as printf cannot be used without the C runtime system, making this approach somewhat pointless for console applications unless you write your own specialist runtime equivalents. This approach does however, make a lot of sense with Windows applications, as it reduces the executable image size and load times significantly, as well as in many cases even execution time.

Project Setup

There are a number of things you have to modify within Visual Studio in order to achieve a C runtime-free executable.

In the project property pages, go to Configuration Properties -> C/C++ -> Code Generation and set the following items:

  • Set 'Enable C++ Exceptions' to No
  • Set 'Basic Runtime Checks' to Default
  • Set 'Buffer Security Check' to No


Go to Configuration Properties -> C/C++ -> Advanced and set the 'Compile As' to 'Compile As C Code (/TC)

Go to the Configuration Properties -> Linker -> Input and set 'Ignore All Default Libraries' to Yes (/NODEFAULTLIB)

Apply the changes made and OK the property page dialog. You are now ready to compile your project without the C runtime environment.

The Executable Entry Point

By default, the linker expects the executable entry point to be WinMainCRTStartup. Unless you change the entry point in the Configuration Properties -> Linker -> Advanced -> Entry Point to some other symbol, you need to write a function called WinMainCRTStartup as the following code snippet demonstrates.

void __stdcall WinMainCRTStartup() {
}



If you try and compile that code as is, your executable will most definitely go illegal. This is because the Windows operating system does not call this function. It is the executable entry point, so although we have cheated by declaring it as a function, the operating system regards it as simply a label. This means that we MUST ensure that ExitProcess is called before the code generated by the compiler tries to return to a non-existent calling routine. the code below is a complete and working example of this.


#include <windows.h>

void __stdcall WinMainCRTStartup() {
    ExitProcess(0);
}



Missing Linker Declarations

If you compile your program and the linker flags errors due to missing functions, this is probably due to your code trying to call a C runtime library routine (and there are many!). Perfect examples of this would be using routines such as strcpy, strcmp etc. So long as you stick to the Windows API calls, especially the Windows string functions (found here) you should not have many problems.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1