user3267021
user3267021

Reputation: 19

Behaviour of precompiled header file causes error

In below simple program,

#include<conio.h>

#include "stdafx.h"
#include<stdio.h>

int main(int argc, _TCHAR* argv[])
{
    printf("print");
    getch();
    return 0;
}

gives error saying 1>e:\cust\a\a\a.cpp(14) : error C3861: 'getch': identifier not found

I am using VS 2005. I know that "stdafx.h" is pre-compiled header, but why we should not add any header files before it?

Upvotes: 1

Views: 159

Answers (1)

Wojtek Surowka
Wojtek Surowka

Reputation: 21003

Precompiled headers in VS work in such a way that the precompiled header (normally named stdafx.h) has to be the first one in your includes. Even more, the line

#include "stdafx.h"

should be the first thing in your source, since MSDN says "The compiler treats all code occurring before the .h file as precompiled. It skips to just beyond the #include directive associated with the .h file, uses the code contained in the .pch file, and then compiles all code after filename. ".

Upvotes: 1

Related Questions