Home > Net >  Including a precompiled header and a non-precompiled header in a .cpp file causes the .cpp file to n
Including a precompiled header and a non-precompiled header in a .cpp file causes the .cpp file to n

Time:05-17

Visual Studio 2022:

I included a simple header to store basic functions like printing text or executing functions to my .cpp file, but after including a precompiled header that stores Windows.h the .cpp file doesn't recognize the functions/variables inside of the non-precompiled header.

CPP:

#pragma once
#include "basics.h"
#include "precompiled header.h"

int main()
{
    Basics::Print("Something"); // C2653 Basics is not a class or namespace name
}

basics.h:

class Basics
{
public:
   static void Print(const char* format, ...);
}

precompiled header.h:

#pragma once
#include <Windows.h> 

// This header is than #included in a .cpp file.
  • What is the point of precompiling the headers if some headers need other headers but can't access them since only .cpp files can? Do you really want to precompile headers Include headers in another headers that need them.

CodePudding user response:

The precompiled header must come first in the include list, because it erases everything that comes before it.

  • Related