Question

I created an empty C++ project in Visual Studio 2012 Express (for Desktop of course), and added some random basic 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're wondering what the hell this is about, I'm disassembling the created executable file to observe the behaviour of the optimizing compiler, and also to learn more about x86 assembly.)

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

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

Runtime Error!

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

R6030

  • CRT not initialized

So, this basic program won't run due to some problem with the runtime library, which I can't figure out!

Any ideas? I'd just like to know what's going on here. Thanks in advance!

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

Was it helpful?

Solution

Start a new project with the "Empty project" template has a knack for causing trouble. You probably changed another project setting that causes your program to start at the main() method instead of the normal entry point, the CRT startup function. Which initializes the CRT, then calls main(). Hard to guess how you did it, especially when you talk about changing a linker setting to get /MT. That's a compiler setting.

Fall in the pit of success by using the Win32 + Win2 Console Application project template instead. Delete the pre-generated code, minus the #include <stdafx.h> line at the top. At the very least you'll now have a starting point that can help us help you solve problems. And don't skip the "Hello world" program.

OTHER TIPS

I fixed the problem, and it's a fault on my part.

I had set the entry point explicitly to main in the linker settings, when it should have been left default.

A console program using the CRT actually has an entry point called _mainCRTStartup, which initializes the CRT before calling the program's main function, which is almost a 'pseudo-entry point'.

If you set the entry point yourself in the linker settings, _mainCRTStartup is never called, so the CRT is never initialized; the program starts off at main and can't execute CRT functions.

I simply removed the explicitly defined entry point and everything worked.

You learn something new every day.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top