Visual Studio 2022:
I want to Include Precompiled headers in my .cpp file but I don't know if it's worth it since I'll also need to include a non-precompiled header with almost the same headers that are in the precompiled header.
Will the non-precompiled header use the precompiled headers or will it generate the code again on each compilation?
CPP:
#pragma once
#include "Precompiled.h"
#include "No-Precompiled.h" // Basic Headers: Windows.h, Psapi.h
int main()
{
// Functions that I need from "No-Precompiled.h" but I can't Precompile it since changes in it are made on regular basis
}
No-Precompiled.h:
#pragma once
#include <windows.h>
#include <Psapi.h>
#include <d3d11.h>
class Template
{
public:
//Functions that need many same Headers.
}
Precompiled.h:
#pragma once
#include <windows.h>
#include <Psapi.h>
#include <d3d11.h>
#include <limits>
#include <complex>
#include <filesystem>
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <tchar.h>
#include <wincred.h>
#include <complex>
#include <math.h>
- Should I just Precompile the headers that the .cpp file uses (which is not much) or is there a way to allow No-Precompiled headers to use the Precompiled headers?
CodePudding user response:
Using pre-compiled headers doesn't change that much. In particular, header guards continue to work. The header guard for <windows.h>
is also included in the pre-compiled state. Hence, when the compiler sees <windows.h>
for the second time, it's immediately skipped.
In your case, the No-Precompiled.h
header turns out to be pretty trivial, as all its headers have already been included. You're just compiling the Template
.
I'd wonder a bit about the particular set of precompiled headers, though. PSapi and DirectX and IOstream? I can't really imagine a big program where you have many files using all of them. Note that <iostream>
is really about std::cout
, which doesn't make a lot of sense for DirectX programs.