4

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.

jalf
  • 243,077
  • 51
  • 345
  • 550
Archimaredes
  • 1,397
  • 12
  • 26
  • You say you compiled and started debugging but you also say that you work in release mode. Are you sure of these two points? – alestanis Oct 20 '12 at 12:12
  • Check this - http://blogs.technet.com/b/virtualworld/archive/2010/07/07/r6030-crt-not-initialized.aspx – user93353 Oct 20 '12 at 12:13
  • 1
    @alestanis it's possible to debug release builds, as long as the build is compiled with debug information turned on; even if it's not you could still attach a debugger, in assembly though – stijn Oct 20 '12 at 12:18
  • @stijn Yes, debug information is on, and I only doing it as a fast way to run the program anyway. – Archimaredes Oct 20 '12 at 12:21
  • @user93353 That isn't really relevant - it's dealing with the linking of third-party DLLs and how they get confused with static/dynamic linking of the CRT, and I'm not using any third-party DLLs. – Archimaredes Oct 20 '12 at 12:22
  • Can you stop debugger while Message Box appears? – Maximus Oct 20 '12 at 12:27
  • 1
    You write "immediately got this error in a message box". Not sure for 100% with 2012 studio, but thought you are trying to create console application but linker think this is GUI. – Maximus Oct 20 '12 at 12:29
  • You can't and shouldn't "close" a question just by adding CLOSED to the title, just because you figured out the answer. Just post your own answer and mark that as accepted if it is better than the existing one. – jalf Oct 20 '12 at 13:15

2 Answers2

2

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

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.

Archimaredes
  • 1,397
  • 12
  • 26