Reputation: 153
How can I completely disable all of the run-time error messages provided by Visual Studio?
I mean, not have them be included in my app at all.
I handle the errors myself in a vectored exception handler, and I do not want to have to keep calling _CrtSetReportMode.
Upvotes: 2
Views: 4631
Reputation: 343
Old question but whatever.
For gcc/g++:
g++ -o name.exe -nostartfiles -nostdlib -nodefaultlibs -l(your_libraries_goes_here) ... -emainCRTStartup -fno-exceptions -fno-builtin -mno-stack-arg-probe -Xlinker --stack=0x200000,0x200000
You can easilly search for any of these option to learn what each one does.
If you are on Linux don't put the .exe
in the executable name and also, in Linux or Windows, you could use another entry point, the -e~
. I like to use mmain
, that way I know it's main
but also not the "original" one; 'm' in this case stands for "mine", a personal joke of mine, no pun intended.
For Visual Studio (Purple):
"Short" answer, this post:
https://hero.handmade.network/forums/code-discussion/t/94-guide_-_how_to_avoid_c_c++_runtime_on_windows
Long answer:
I don't like how some things were done in the post (link above), but I like how this video did (he looks at the post in the video):
Watch this video (3h long, but worth it, also, haven't watched the entire video, but even when he says he is finished with removing the CRT stuff he will talk about the potential problems that you can find, or read this answer first :D): https://www.youtube.com/watch?v=sE4tUVaxiV0
I will give the steps for Visual Studio's compiler, for other compilers I don't know how it would work.
Also also, I'm Brazilian, so if the command names etc don't match exactly, just go to the closest.
First, let's do all the changes in configs that you need to do:
You need to change the subsystem in the configuration of your project to your need, in case of windows it will be:
I don't know how it is for Linux.
Set so it doesn't include additional libraries:
All other libraries that you need you can either link using
#pragma comment(lib, "lib name here using the quotation marks")
or you can use the field above of the "Ignore default libs".
Remove all checking stuff of Visual Studio: I don't know if you need to remove all of this, but if you are like me, since you are removing the CRT, you probably don't want any type of checking or whatever. Anyway, here is my complete list of things:
Second, your entry point; note that it's not WinMain(...)
; it's WinMainCRTStartup()
, like this:
// No parameters
DWORD WINAPI WinMainCRTStartup()
{
...
}
WINAPI is just a macro for __stdcall
.
Technically the definition of it is using DWORD
, but I normally use int
.
The docs say that you should use ExitProcess(_return value_);
to exit the code, I myself do that, and if you do that you don't even need to use return ~
; even though some people would say it's good practice, I just think it's useless.
And last thing:
Compilers nowadays love to use the CRT, which I hate. So if you try to compile code with zero stuff in it with everything about it will work, but you may have the problem with some more stuff:
You need to define your own memset()
and define a _fltused
, memeset
is memset
, the compiler uses it for when you create big arrays and _fltused
(float used, didn't know that) when you use float
stuff.
To be honest, just copy this file I made, also, Visual Studio doesn't like if you do that on a header file, so do that on a .c file mine_msvc.c
for example:
extern "C"
{
// Don't actually know if you need the value, since looks like it works even without it, I myself leave it with the value.
int _fltused = 0x9875;
#pragma warning( push )
#pragma warning( disable : 28251 )
#pragma warning( disable : 6001 )
#pragma function(memset)
void* __cdecl memset(void* dest, int val, size_t tam)
{
char* retDest = (char*)dest;
while (tam--)
*retDest++ = (char)val;
return retDest;
}
}
#pragma warning( pop )
Also, remember basic C/C++ stuff, you can't just use this in every place, because of multiple definitions, I myself use it in my library and I have an EXTERN
macro, but you can do a header file:
extern "C"
{
extern int _ftlused;
void* __cdecl memset(void* dest, int val, size_t tam);
}
I would say this is all (look at the final paragraph), what I used to do to check if there was really any more stuff would be to use "Dependencies.exe", but for me at least it just got less reliable, so what you can do is to use IDA or Ghidra, I don't know how to use Ghidra and I just use the free version of IDA.
At least in "IDA" in the left panel you can see functions, everything that is called sub_~number~
is yours, and you can even use your assembly knowledge and check the functions, some other stuff may not be called like that, for example in my case I had a Windows hook called fn
and a thread called StartAddress
, plus the actual start
. Now, if you see a lot of functions with weird names, something got linked without you asking.
I personally don't know too much how the executable is composed, but in "IDA" you can look at near the end of the file, or search for the place that says
;
; Import names for "dll name"
;
and see extra functions that your code uses, most, if not all, will be Windows obligatory functions or the API functions that you used.
And just to finish, if you use that, of course you can't use anything from the CRT, but you also can't use new
nor delete
(unless you overload them), you can still use STL, since they are templates, I personally don't like them and don't use them; you can't use C++ error handling, since it's part of the CRT.
At the time Im writing this, I still didn't had any problem using these options, but you can have extra problems, like stack size, thread stack sizes, etc. About that, read the link or watch the video, because for example, for the stack size check you could implement your own __chkstk
, but I don't know how to do that + since nowadays 1MB is nothing, you can just use compiler options to ask for the 1MB since the start; again, just read the first link.
Upvotes: 4
Reputation: 4040
>>How to disable CRT completely
As far as I'm concerned, in generally you couldn't disable CRT completely:
1,Entry point (gets input from console)
2,Provide common functions used in C and C++
If you want to compile your app without C-Runtime Library (CRT) ,I suggest you could try to use /MT
, /NODEFAULTLIB
linker options and redefine entry point at Linker -> Advanced -> Entry Point
to function defined in your code.
Upvotes: -1