What is "CRT not initialized" with this error?

I created an empty C ++ project in Visual Studio 2012 Express (for the desktop, of course) and added some random base code:

#include <cstdio> #include <cstdlib> typedef struct examplestruct { unsigned char num1; unsigned short num2; unsigned long num3; unsigned long long num4; } EXAMPLESTRUCT; void examplefunction(unsigned long *num, int num2) { *num += num2; return; } int main(int nArgs, char **pszArgs) { EXAMPLESTRUCT ExStructInstance = {0xFF, 0xFFFF, 0xFFFFFFFF, 0xFFFFFFFFFFFFFFFF}; printf("%d, %d, %u, %ull\n", ExStructInstance.num1, ExStructInstance.num2, ExStructInstance.num3, ExStructInstance.num4); unsigned long num5 = ExStructInstance.num1 + ExStructInstance.num2; printf("%d\n", num5); examplefunction(&num5, 10); printf("%d\n", num5); system("pause"); return 0; } 

(If you are interested in what this refers to, I will parse the generated executable file to observe the behavior of the optimizing compiler, as well as learn more about the x86 assembly.)

In Linker, in the project settings, I selected Multi-threaded (/MT) for the runtime library, so it statically links it.

I compiled and started debugging with F5 and immediately got this error in the message box:

Runtime Error!

Program: C: \ Users \ xxxxx \ Documents \ P ...

R6030

  • CRT not initialized

So, this basic program will not start due to some problems with the runtime library, which I cannot understand!

Any ideas? I just would like to know what is going on here. Thanks in advance!

EDIT: FYI, all this is done in Release mode.

+4
source share
2 answers

Launch a new project with the "Empty project" template, which may cause problems. You probably changed another project parameter that causes your program to run using the main () method instead of the usual entry point, the CRT launch function. Which initializes the CRT and then calls main (). It's hard to guess how you did it, especially when you are talking about changing the linker settings to get / MT. This is a compiler setting.

Success will disappear, using the Win32 + Win2 Console Application project template instead. Remove the pre-generated code minus the #include <stdafx.h> line at the top. At least you will have a starting point that will help us solve problems. And do not miss the "Hello world" program.

+2
source

I fixed the problem, and this is a mistake on my part.

I set the entry point explicitly in main in the linker settings, when it was supposed to stay by default.

A console program that uses a CRT actually has an entry point called _mainCRTStartup , which initializes the CRT before calling the main function of the program, which is almost a “pseudo- _mainCRTStartup point”.

If you yourself set an entry point in the linker settings, _mainCRTStartup never called, so CRT is never initialized; the program runs in main and cannot perform CRT functions.

I just deleted the explicitly defined entry point and it worked.

Every day you learn something new.

+1
source

Source: https://habr.com/ru/post/1440955/


All Articles