[4.18]*.h must be first header when packaging plugin

Hi there,

I’m currently attempting to update my plugin to be compatible with 4.18. However, when I try to package it, I get a series of errors telling me that all of my .cpp files must include their respective headers first. However, I must also include the same precompiled header for all classes as the first include file. How can I get past this catch-22?

Example #includes in my Deathmatch.cpp:

#include "FPSWizardPrivatePCH.h"
#include "Deathmatch.h"
#include "FPSHUD.h"
#include "FPSCharacter.h"
#include "MasterWeapon.h"
#include "FPSPlayerState.h"
#include "FPSGameState.h"
#include "FPSSaveGame.h"
#include "WeaponData.h"

Plugin packaging log:

    [2017.10.25-18.49.29:965][345]UATHelper: Package Plugin Task (Windows): Running AutomationTool...
    [2017.10.25-18.49.30:610][383]LogDirectoryWatcher: A directory notification for '../../../Engine/Plugins/' was aborted.
    [2017.10.25-18.49.30:611][383]LogDirectoryWatcher: A directory notification for '../../../../../../Users/cw_si/Documents/Unreal Projects/NewProject/Plugins/' was aborted.
    [2017.10.25-18.49.33:630][565]UATHelper: Package Plugin Task (Windows): C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\Microsoft.Common.CurrentVersion.targets(1767,5): warning : The referenced project '..\IOS\MobileDeviceInterface\MobileDeviceInterface.csproj' does not exist. [D:\Program Files\Epic Games\UE_4.18\Engine\Source\Programs\AutomationTool\AutomationTool.csproj]
    [2017.10.25-18.49.34:946][644]UATHelper: Package Plugin Task (Windows): Parsing command line: BuildPlugin -Plugin="D:/Users/cw_si/Documents/Unreal Projects/NewProject/Plugins/FPSWizard/FPSWizard.uplugin" -Package=D:/FPSWizardItch[4.18]/FPSWizard -CreateSubFolder -compile
    [2017.10.25-18.49.35:147][656]UATHelper: Package Plugin Task (Windows): Compiling scripts.
    [2017.10.25-18.49.36:813][756]UATHelper: Package Plugin Task (Windows):     DotNETUtilities -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\DotNETUtilities.dll
    [2017.10.25-18.49.36:896][761]UATHelper: Package Plugin Task (Windows):     UnrealBuildTool -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\UnrealBuildTool.exe
    [2017.10.25-18.49.37:064][771]UATHelper: Package Plugin Task (Windows):     AutomationUtils.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationUtils.Automation.dll
    [2017.10.25-18.49.37:130][775]UATHelper: Package Plugin Task (Windows):     AllDesktop.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\AllDesktop\AllDesktop.Automation.dll
    [2017.10.25-18.49.37:330][787]UATHelper: Package Plugin Task (Windows):     Localization.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\Localization.Automation.dll
    [2017.10.25-18.49.37:430][793]UATHelper: Package Plugin Task (Windows):     OneSkyLocalization.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\OneSkyLocalization.Automation.dll
    [2017.10.25-18.49.37:547][800]UATHelper: Package Plugin Task (Windows):     AutomationScripts.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\AutomationScripts.Automation.dll
    [2017.10.25-18.49.37:696][809]UATHelper: Package Plugin Task (Windows):     Android.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\Android\Android.Automation.dll
    [2017.10.25-18.49.44:170][175]UATHelper: Package Plugin Task (Windows):     BuildGraph.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\BuildGraph.Automation.dll
    [2017.10.25-18.49.44:272][181]UATHelper: Package Plugin Task (Windows):     HTML5.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\HTML5\HTML5.Automation.dll
    [2017.10.25-18.49.44:320][184]UATHelper: Package Plugin Task (Windows):   C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\Microsoft.Common.CurrentVersion.targets(1767,5): warning : The referenced project '..\..\IOS\MobileDeviceInterface\MobileDeviceInterface.csproj' does not exist. [D:\Program Files\Epic Games\UE_4.18\Engine\Source\Programs\AutomationTool\IOS\IOS.Automation.csproj]
    [2017.10.25-18.49.44:405][189]UATHelper: Package Plugin Task (Windows):     IOS.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\IOS\IOS.Automation.dll
    [2017.10.25-18.49.44:487][194]UATHelper: Package Plugin Task (Windows):     Linux.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\Linux\Linux.Automation.dll
    [2017.10.25-18.49.44:573][199]UATHelper: Package Plugin Task (Windows):     Mac.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\Mac\Mac.Automation.dll
    [2017.10.25-18.49.44:671][205]UATHelper: Package Plugin Task (Windows):     TVOS.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\TVOS\TVOS.Automation.dll
    [2017.10.25-18.49.45:271][241]UATHelper: Package Plugin Task (Windows):     Win.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\Win.Automation.dll
    [2017.10.25-18.49.45:354][246]UATHelper: Package Plugin Task (Windows):     XLocLocalization.Automation -> D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\AutomationScripts\XLocLocalization.Automation.dll
    [2017.10.25-18.49.45:389][248]UATHelper: Package Plugin Task (Windows): Took 9.8747061s to run MSBuild.exe, ExitCode=0
    [2017.10.25-18.49.46:954][342]UATHelper: Package Plugin Task (Windows): Copying 288 file(s) using max 64 thread(s)
    [2017.10.25-18.49.47:225][358]UATHelper: Package Plugin Task (Windows): Reading plugin from D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\FPSWizard.uplugin...
    [2017.10.25-18.49.47:306][363]UATHelper: Package Plugin Task (Windows): Building plugin for host platforms: Win64
    [2017.10.25-18.49.47:341][365]UATHelper: Package Plugin Task (Windows): Running: D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\UnrealBuildTool.exe UE4Editor Win64 Development -project="D:\FPSWizardItch[4.18]\FPSWizard\HostProject\HostProject.uproject" -iwyu -module FPSWizard -receipt D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Binaries\Win64\UE4Editor.target -NoHotReload
    [2017.10.25-18.49.47:558][378]UATHelper: Package Plugin Task (Windows):   Performing full C++ include scan (building a new target)
    [2017.10.25-18.49.47:574][379]UATHelper: Package Plugin Task (Windows):   Creating makefile for UE4Editor (no existing makefile)
    [2017.10.25-18.50.00:710][167]UATHelper: Package Plugin Task (Windows):   Parsing headers for UE4Editor
    [2017.10.25-18.50.00:744][169]UATHelper: Package Plugin Task (Windows):     Running UnrealHeaderTool "D:\FPSWizardItch[4.18]\FPSWizard\HostProject\HostProject.uproject" "D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Intermediate\Build\Win64\UE4Editor\Development\UE4Editor.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -installed
    [2017.10.25-18.50.15:248][ 39]UATHelper: Package Plugin Task (Windows):   Reflection code generated for UE4Editor in 14.4957513 seconds
    [2017.10.25-18.50.15:686][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\BulletShell.cpp(1): error: Expected BulletShell.h to be first header included.
    [2017.10.25-18.50.15:686][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\CTF.cpp(1): error: Expected CTF.h to be first header included.
    [2017.10.25-18.50.15:686][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\Deathmatch.cpp(1): error: Expected Deathmatch.h to be first header included.
    [2017.10.25-18.50.15:687][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\Explosion.cpp(1): error: Expected Explosion.h to be first header included.
    [2017.10.25-18.50.15:687][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\Flag.cpp(1): error: Expected Flag.h to be first header included.
    [2017.10.25-18.50.15:687][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\FlagReturn.cpp(1): error: Expected FlagReturn.h to be first header included.
    [2017.10.25-18.50.15:687][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\FPSCharacter.cpp(1): error: Expected FPSCharacter.h to be first header included.
    [2017.10.25-18.50.15:688][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\FPSGameState.cpp(1): error: Expected FPSGameState.h to be first header included.
    [2017.10.25-18.50.15:688][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\FPSHUD.cpp(1): error: Expected FPSHUD.h to be first header included.
    [2017.10.25-18.50.15:688][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\FPSPlayerState.cpp(1): error: Expected FPSPlayerState.h to be first header included.
    [2017.10.25-18.50.15:688][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\FPSSaveGame.cpp(1): error: Expected FPSSaveGame.h to be first header included.
    [2017.10.25-18.50.15:689][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\FPSWizard.cpp(1): error: Expected FPSWizard.h to be first header included.
    [2017.10.25-18.50.15:689][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\Grenade.cpp(1): error: Expected Grenade.h to be first header included.
    [2017.10.25-18.50.15:689][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\GrenadeWeapon.cpp(1): error: Expected GrenadeWeapon.h to be first header included.
    [2017.10.25-18.50.15:689][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\HealthPack.cpp(1): error: Expected HealthPack.h to be first header included.
    [2017.10.25-18.50.15:690][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\MasterWeapon.cpp(1): error: Expected MasterWeapon.h to be first header included.
    [2017.10.25-18.50.15:690][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\Projectile.cpp(1): error: Expected Projectile.h to be first header included.
    [2017.10.25-18.50.15:690][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\TeamStart.cpp(1): error: Expected TeamStart.h to be first header included.
    [2017.10.25-18.50.15:690][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\Territories.cpp(1): error: Expected Territories.h to be first header included.
    [2017.10.25-18.50.15:691][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\Territory.cpp(1): error: Expected Territory.h to be first header included.
    [2017.10.25-18.50.15:691][ 65]UATHelper: Package Plugin Task (Windows):   D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Source\FPSWizard\Private\WeaponData.cpp(1): error: Expected WeaponData.h to be first header included.
    [2017.10.25-18.50.15:691][ 65]UATHelper: Package Plugin Task (Windows):   ERROR: Build canceled.
    [2017.10.25-18.50.15:692][ 65]UATHelper: Package Plugin Task (Windows): Took 28.3658254s to run UnrealBuildTool.exe, ExitCode=1
    [2017.10.25-18.50.15:735][ 68]UATHelper: Package Plugin Task (Windows): ERROR: Command failed (Result:1): D:\Program Files\Epic Games\UE_4.18\Engine\Binaries\DotNET\UnrealBuildTool.exe UE4Editor Win64 Development -project="D:\FPSWizardItch[4.18]\FPSWizard\HostProject\HostProject.uproject" -iwyu -module FPSWizard -receipt D:\FPSWizardItch[4.18]\FPSWizard\HostProject\Plugins\FPSWizard\Binaries\Win64\UE4Editor.target -NoHotReload. See logfile for details: 'UnrealBuildTool-2017.10.25-11.49.47.txt' 
    [2017.10.25-18.50.15:735][ 68]UATHelper: Package Plugin Task (Windows):        (see D:\Program Files\Epic Games\UE_4.18\Engine\Programs\AutomationTool\Saved\Logs\UAT_Log.txt for full exception trace)
    [2017.10.25-18.50.15:853][ 75]UATHelper: Package Plugin Task (Windows): AutomationTool exiting with ExitCode=1 (Error_Unknown)
    [2017.10.25-18.50.15:900][ 77]UATHelper: Package Plugin Task (Windows): BUILD FAILED
    [2017.10.25-18.50.28:036][801]LogWindowsTextInputMethodSystem: Display: IME system now deactivated.
    [2017.10.25-18.50.42:129][801]LogStats: FPlatformStackWalk::StackWalkAndDump - 14.058 s
    [2017.10.25-18.50.42:129][801]LogOutputDevice: Error: === Handled ensure: ===
    [2017.10.25-18.50.42:130][801]LogOutputDevice: Error: 
    [2017.10.25-18.50.42:130][801]LogOutputDevice: Error: Ensure condition failed: TextureRHI [File:D:\Build\++UE4+Release-4.18+Compile\Sync\Engine\Source\Runtime\Windows\D3D11RHI\Private\D3D11RenderTarget.cpp] [Line: 938]
    [2017.10.25-18.50.42:130][801]LogOutputDevice: Error: 
    [2017.10.25-18.50.42:130][801]LogOutputDevice: Error: Stack: 
    [2017.10.25-18.50.42:130][801]LogOutputDevice: Error: [Callstack] 0x0000000024501EA6 UE4Editor-Core.dll!FWindowsPlatformStackWalk::StackWalkAndDump() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\windows\windowsplatformstackwalk.cpp:200]
    [2017.10.25-18.50.42:130][801]LogOutputDevice: Error: [Callstack] 0x00000000242A121A UE4Editor-Core.dll!FDebug::EnsureFailed() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\misc\assertionmacros.cpp:298]
    [2017.10.25-18.50.42:130][801]LogOutputDevice: Error: [Callstack] 0x00000000242BB826 UE4Editor-Core.dll!FDebug::OptionallyLogFormattedEnsureMessageReturningFalse() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\misc\assertionmacros.cpp:425]
    [2017.10.25-18.50.42:131][801]LogOutputDevice: Error: [Callstack] 0x000000001729ADA6 UE4Editor-D3D11RHI.dll!FD3D11DynamicRHI::RHIReadSurfaceData() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\windows\d3d11rhi\private\d3d11rendertarget.cpp:938]
    [2017.10.25-18.50.42:131][801]LogOutputDevice: Error: [Callstack] 0x0000000046371EF2 UE4Editor-RHI.dll!FRHICommandListImmediate::ReadSurfaceData() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\rhi\public\rhicommandlist.h:4014]
    [2017.10.25-18.50.42:131][801]LogOutputDevice: Error: [Callstack] 0x00000000228C11DA UE4Editor-Engine.dll!TGraphTask<`FRenderTarget::ReadPixels'::`2'::EURCMacro_ReadSurfaceCommand>::ExecuteTask() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\public\async\taskgraphinterfaces.h:784]
    [2017.10.25-18.50.42:132][801]LogOutputDevice: Error: [Callstack] 0x0000000024164825 UE4Editor-Core.dll!FNamedTaskThread::ProcessTasksNamedThread() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:651]
    [2017.10.25-18.50.42:132][801]LogOutputDevice: Error: [Callstack] 0x0000000024164E0A UE4Editor-Core.dll!FNamedTaskThread::ProcessTasksUntilQuit() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:560]
    [2017.10.25-18.50.42:133][801]LogOutputDevice: Error: [Callstack] 0x00000000490322C4 UE4Editor-RenderCore.dll!RenderingThreadMain() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\rendercore\private\renderingthread.cpp:327]
    [2017.10.25-18.50.42:133][801]LogOutputDevice: Error: [Callstack] 0x00000000490332F6 UE4Editor-RenderCore.dll!FRenderingThread::Run() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\rendercore\private\renderingthread.cpp:461]
    [2017.10.25-18.50.42:133][801]LogOutputDevice: Error: [Callstack] 0x000000002450004C UE4Editor-Core.dll!FRunnableThreadWin::Run() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\windows\windowsrunnablethread.cpp:76]
    [2017.10.25-18.50.42:133][801]LogOutputDevice: Error: [Callstack] 0x00000000244F1C83 UE4Editor-Core.dll!FRunnableThreadWin::GuardedRun() [d:\build\++ue4+release-4.18+compile\sync\engine\source\runtime\core\private\windows\windowsrunnablethread.cpp:33]
    [2017.10.25-18.50.42:133][801]LogOutputDevice: Error: [Callstack] 0x000000006DF81FE4 KERNEL32.DLL!UnknownFunction []
    [2017.10.25-18.50.42:134][801]LogOutputDevice: Error: [Callstack] 0x000000006FCBEF91 ntdll.dll!UnknownFunction []
    [2017.10.25-18.50.42:134][801]LogOutputDevice: Error: 
    [2017.10.25-18.50.42:134][801]LogStats:                SubmitErrorReport -  0.000 s

I solved ensuring that the XXX.h files are the first includes of the corresponding XXX.cpp files; then adding:

PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;

in the <plugin>.build.cs file and including the PCH header file where needed.

Experienced the same issue with 4.18.1. Adding

 PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;

to the .build.cs file was enough for me.