Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile C++ without CRT

Tags:

c++

native

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.

like image 287
Prionum Avatar asked Apr 14 '16 15:04

Prionum


5 Answers

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.

like image 80
jstine Avatar answered Nov 14 '22 23:11

jstine


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.

like image 38
CoffeDeveloper Avatar answered Oct 26 '22 00:10

CoffeDeveloper


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.

like image 2
OMGtechy Avatar answered Nov 14 '22 23:11

OMGtechy


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

like image 2
user2997204 Avatar answered Nov 14 '22 22:11

user2997204


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!

like image 2
insaneasusual Avatar answered Nov 14 '22 21:11

insaneasusual