Adding a new CPP file to project causes other classes to be undefined

I have no idea what’s happening anymore. I’ve tried everything I can think of with no luck. I have been trying to add a new class to my existing project but every time I try to compile it in visual studio, I get 100+ compiler errors which have nothing to do with the class I just added.

Environment:
Visual Studio Community Edition 2013
Engine: UE 4.6.1
Source control: Perforce
OS: 64 bit

Control Scenario 1: I compile my project with my existing classes.
Result: Everything compiles successfully and the editor opens.

Scenario 2: I add a brand new header file and do not declare anything.
Result: Everything compiles successfully and the editor opens

Scenario 3: I create a bare essentials CPP file to go with the header file.
Result: The compile fails. There are no errors in my code. One of my other classes is now unrecognized and that’s causing 100+ errors (not linker errors).
Removing the newly added CPP file causes the errors to go away, so there’s some sort of relationship going on here.

Scenario 4: I just create a new blank CPP file with no header file. The only line it has: #include “[projectname].h”
Result: Same as scenario #3.

Scenario 5: I create a brand new project and add a new header and CPP file, just like I do in Scenario 3 &4
Result: Compiles with no issues. Therefore, its project specific.

I tried adding the class through both the editor and manually through visual studio. Both cause the same results.

What I’ve tried:

  • I looked at the [ProjectName].generated.cpp file between scenario 1 and scenario 3 and ran WinDiff against them
    to see if there was anything usual in the generated code, but nothing stood out as looking usually out of place.
  • I completely clean the project and rebuild it. No change.
  • I completely delete all generated files and OBJ files. No change.
  • I’ve tried to create classes with different names and inheriting from different objects. No change.

I’ve added dozens of sources files to the project in the past and none of those have caused any issues. It seems that this started happening recently after I submitted my last build to source control. I tried checking out all of my files just in case there was some write protection issues, but that made no change.

Why would my code compile with no issues, but adding a blank CPP file to the project causes it to break? All I can think is that somewhere, the generated code is getting messed up by a new CPP file and I don’t know where its happening or how to fix it.

Header file: “TestClass.h”

#pragma once

#include "TestClass.generated.h"

UCLASS()
class ATestClass : public AActor
{
	GENERATED_BODY()
public:

};

CPP File: “AnyName.cpp”

#include "MageMaster2.h"

After lots of rigorous testing over two days, I have discovered the following:

Root cause of my issue:
I had two project header files.
#1 was “[projectname].h” and located at “/[projectname]/source/[projectname]/[projectname].h”
#2 was also “[projectname].h” and located at “/[projectname]/source/[projectname]/public/[projectname.h]”

Each header file was different (I forgot to delete one a long time ago).

Oddly, this didn’t raise any errors. However, it did cause some really weird behaviors which are as follows:
If I had 29 CPP files in my project, the UBT would successfully build the whole project. It didn’t matter what kind of CPP files they were. At one point, I backed up and deleted all of my source code and created 100+ empty CPP/H files via a script to see if something else was going on. Through empirical testing, I found that the 30th class always caused a series of build errors. The class code was irrelevant. It appeared as if the CPP files could no longer find their associated .H files. Yet, removing one class seemed to fix the issue. However, there’s no way you can work with a fixed limit on classes.

For some unknown reason (unknown to me anyways), the UBT would seem to “overflow” or something when there were too many classes and it would send the overflowing classes to my other header file which wasn’t including [projectname]Classes.h.
This caused the CPP files to not know about their header files, and everything suddenly and mysteriously looks like the header file is missing.

Solution: Look for duplicate [ProjectName].h header files in all of your source code folders and subfolders and delete them. The “pragma once” preprocessor directive won’t help you here.

Hi Slayemin,

Sorry for not getting a response for your post sooner. I just tried to replicate this issue in 4.6.1, but was unable to see the results that you described. I copied the project header file from the Public folder into the Source/ProjectName folder so that I had two header files with the same name. I then proceeded to add 35 new classes to the project (both header and source code files), and built the project again in Visual Studio without any problems. Do you happen to recall any additional information about this issue that might help us track it down to prevent it from happening again?

  • You mention a [ProjectName]Classes.h file. Is this a file that you have added to your project?
  • Were you adding classes to your project using the Editor, or were you adding files to the project through Visual Studio?

Hey ,

I appreciate the follow up. Chalk this one up to human error on my part (which is more often the case than not).

So, what I was doing was including two header files which had the same name but one was way out of date. ([projectname].h)

  • During my “learning the
    engine/environment” stage, I was
    manually creating the
    [Projectname]Classes.h file and
    adding #includes to it as I added new classes. Later I found
    it was automatically generated for me
    in the intermediate folders and I didn’t need to do this.
  • I manually add classes to my project using visual studio. I make sure that all of the source files are in the appropriate public/private folders since VS tends to default to the intermediate folder.

The outdated header file:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Engine.h"

The up to date header file:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Engine.h"
#include "SlateBasics.h"
#include "SlateExtras.h"
#include "MageMaster2Classes.h"

After some threshold of CPP files, the UBT would switch to using the outdated header file, which didn’t include the “MageMaster2Classes.h” file which contained a list of includes for all of my other game classes. That caused a bunch of linker errors. I’m guessing internally it uses multithreading for compiling and linking and there’s a fixed number of cpp files it will process before splitting the work load into two threads?

Anyways, the solution was to just delete the outdated header file (which was hard to figure out as being the root cause).