10

Basically the title says it all; I want to COMPLETELY disable the CRT when I am coding in C++.

I want to be able to run the exe on Win98 as well as all the other ones. Just plain NATIVE C++.

The compiler I am using is Visual Studio. However, I am willing to change if it's required.

Prionum
  • 133
  • 1
  • 2
  • 7
  • 3
    Out of interest, why do you want to disable the CRT on Windows 98? – Sean Apr 14 '16 at 15:15
  • I phrased it badly; I want to compile the executable without using CRT. – Prionum Apr 14 '16 at 15:16
  • 2
    Sure, but I guess I'm interested in why you're trying to do this. – Sean Apr 14 '16 at 15:18
  • 8
    The answer is almost certainly "you can't". But back up a stage and explain to us (a) what your program does in broad outline, (b) the concrete problems you are running into, (c) why you think disabling the CRT will solve them, and (d) why you think you need to support operating systems that are past their vendor end-of-life. – zwol Apr 14 '16 at 15:18
  • 2
    Programs with no input and no output can be of little interest. –  Apr 14 '16 at 15:18
  • 1
    If you don't understand why I am asking these questions, please read https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem carefully. – zwol Apr 14 '16 at 15:22
  • Maybe the OP is got the wrong term and is talking about CLR instead of CRT. – drescherjm Apr 14 '16 at 15:22
  • The problem I am running to is that my program won't run on win2k: ".exe is not a valid win32 application" -- I thought removing CRT will fix it, but perhaps there is another fix. – Prionum Apr 14 '16 at 15:22
  • @drescherjm I keep on accidentally hitting enter when I try to make a line break. (forgetting to press SHIFT) -- Fixed now. – Prionum Apr 14 '16 at 15:24
  • You probably have to use an older version of Visual Studio. VS 2013 supported XP via the v120_xp toolset but I am not sure that supported win2k. – drescherjm Apr 14 '16 at 15:26
  • 1
    By no means removing CRT is going to help you. It's just that modern Visual C++ compiler can't create executables for Win98. And think about it - Win98 is 20 years ago! Why even think about supporting it? – SergeyA Apr 14 '16 at 15:28
  • 1
    "my program won't run on win2k". It will also not work on Linux or Mac OS X. No big deal, people somehow cope with this. If you want it on win2k, build a version for win2k using a win2k toolchain. – n. m. could be an AI Apr 14 '16 at 15:29
  • @n.m., I guess, one would be hard-pressed to find the toolchain for win2k nowadays. – SergeyA Apr 14 '16 at 15:30
  • @SergeyA I think you can find them on NSDN. – n. m. could be an AI Apr 14 '16 at 15:33
  • You can still get Visual C++ 2.0 and VS.net 2002 among others from MSDN – Rob K Apr 14 '16 at 15:37
  • `The problem I am running to is that my program won't run on win2k: ".exe is not a valid win32 application" -- I thought removing CRT will fix it` then you're having an XY problem like above. The message means the exe file is corrupted, or just isn't x86 binary so obviously it can't be run – phuclv Apr 14 '16 at 15:49
  • Related / Maybe duplicate: [Visual Studio 2015: Compile C/C++ without a runtime library](https://stackoverflow.com/questions/39217241/visual-studio-2015-compile-c-c-without-a-runtime-library/39220245) – jrh Feb 11 '19 at 21:59
  • 5
    Also replying to all the "XY problem" / "You can't" commenters, for the stated question of "C++ without CRT" (I am not sure about Win98 compatibility), [here's a CodeProject article](https://www.codeproject.com/articles/15156/tiny-c-runtime-library) that goes into some of the reasoning as to why somebody might want to do this, and some source code showing how to do it. Sometimes topics like this are just interesting conceptually in their own right, even if most programs won't be built with custom runtimes. – jrh Feb 12 '19 at 20:59

6 Answers6

13

You can remove all references to the CRT provided by Microsoft and replace it with your own custom lightweight CRT. Instructions to do so are somewhat lengthy, and are summed up nicely with accompanying source code here:

https://www.codeproject.com/articles/15156/tiny-c-runtime-library

The instructions are a bit out-of-date, but I managed to get portions of the tlibc to work on Visual Studio 2017 and Windows 10. Everything except the FILE API seems to compile and work. The jist of steps to follow were:

  1. Create a Console Application project.
  2. Disable linking of all standard libraries (/NODEFAULTLIB)
  3. Disable all Compiler Runtime Checks (RTC)

At that point, the tilbc sources compiled and ran (sans outdated FILE portions).


That said, the OP's actual problem was something very different. The CRT is not to blame for the error .exe is not a valid win32 application. This error occurs long before any code from the binary is actually executed, which includes the CRT executed prior to main()/winmain(). It most typically occurs when trying to run an x64 binary on an x86 operating system. In theory even the Visual Studio 2017 toolchain can build applications suitable for Windows 2000 and Win98, so long as you use the correct SDK version flags when including <windows.h>.

Even in cases where the wrong SDK or compiler flags are used, the error reported would be something else -- most likely a failed DLL load or a popup error reading something along the lines of "the program could not start" and followed by a compatibility mode offering.

jstine
  • 3,234
  • 20
  • 21
3

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

user2997204
  • 1,344
  • 2
  • 12
  • 24
2

You can statically link the C++ runtime using the correct project settings, but even then you'll at best get it working on Windows XP.

You can find out how to statically link the standard library here, but just in case the link dies:

To set this compiler option in the Visual Studio development environment

1) Open the project's Property Pages dialog box. For details, see How to: Open Project Property Pages.

2) Expand the C/C++ folder.

3) Select the Code Generation property page.

4) Modify the Runtime Library property.

If you really want to get it working on Windows 98, then you need to use an older version of Visual Studio and do something like the above.

OMGtechy
  • 7,935
  • 8
  • 48
  • 83
  • 1
    VC++ very much uses CRT for native C++ code, the library in question is called `msvcrt.dll`. Note the three letters `c` `r` and `t` in the middle. – n. m. could be an AI Apr 14 '16 at 15:26
  • How do I make my exe able to run on older Windows versions then? I want it to support all Windows versions. – Prionum Apr 14 '16 at 15:27
  • @Prionum, see my answer, basically you need a compiler that support the windows version you are targeting and you need to do at least some different builds. I'm not even sure most recent Visual Studio still supports win 98. You should probably put a new question asking wich compilers support Win98 (or the systems you want) and how to setup correct project settings. – CoffeDeveloper Apr 14 '16 at 15:34
  • See if you can purchase a copy of Visual Studio 2003. – drescherjm Apr 14 '16 at 15:37
  • 2
    @DarioOO last I checked, the more recent version of Visual Studio include special support for XP if you ask for it, but no earlier than that. – OMGtechy Apr 15 '16 at 09:10
  • yes win 2k or less is TOO old (XP is already too old, and vista by the way is bad supported anywhere) – CoffeDeveloper Apr 15 '16 at 09:43
  • I still have a lot of sympathy for both software vendors who feel they still need to support XP, and end-users who feel they still cannot afford to move to something newer than XP. But Win98 and Win2K are Right Out. – zwol Apr 15 '16 at 15:34
2

It's quite possible, however, there are many disadvantages so I do not recommend it (I will not be getting into them).

Do you have a reason for targeting Win98? Almost no one uses it anymore. I recommend Windows Vista+, however, it's entirely up to you.

First of all, you will need to disable Optimization, set the RT lib to MT, disable security checks (this will make your application less secure!), among other things. To do that go to C/C++ settings and add /Od /MT /GS- to the command line.

Next go to Code Generation and set Enable C++ Exceptions to No. After that go to linker and add /NODEFAULTLIB to the command line. Finally, navigate to Advanced and set the entry point.

After that you're good to go!

phuclv
  • 37,963
  • 15
  • 156
  • 475
1

It's hard helping without knowing what you need to do, in general case you cannot remove CRT, here's why:

  • Entry point (gets input from console)
  • Provide common functions used in C and C++

That means that without CRT you won't be able to use neither stdio (unless you link it later manually), also you cannot run the application in the console.

A way you can remove CRT is by creating a static library, this is near to pure native C++ as much as possibile, if you don't link external libraries inside it (well, a object file is much more near to navite C++ than a static library).

Depending on operative system you may not even be able to call a function within the binary without the CRT (in example Windows). So if you want just to avoid binary overhead the best bet is having a static/dynamic library wich is linked to some other "launcher" or just dynamically invoked (that needs a way to retrieve an entry point anyway).

Again, hard to tell what you need if you don't give enough details.

EDIT:

If you need to run on older win98 then use a compiler that supports win98 and specify you want to compile for win98 using compiler flags.

CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
1

You can try using different compiler's such as Symantec C++ 7.5 or Digital Mars C++.

I have programs made by the Symantec compilers that run flawlessly even in Win 11.

In fact hats-off to the author of the 90'a app pfe32 (programmers file editor) that runs' on any Windows version.