Including .cpp files in build.cs

Hey,
in my project I want to include external code, that wasn’t written by me. It is all .cpp and .h files lying in a repository.
I successfully included a single header file for testing purposes, but unfortunately it gives me a linking error.
I know how to link static and dynamic libraries in the Build.cs file, but how do I tell UBT where the .cpp file is?
The .cpp file is in the same directory as the header file, so I thought that maybe UBT is able to find the .cpp file itself, but apparently this isn’t the case.

So is there something like the “PublicAdditionalLibraries.Add(…)”, but for .cpp files?

Copying the files to the project folder would obviously solve that issue, but unfortunately this isn’t a solution.

I hope someone can help me with that :slight_smile:

Hi zapfdk,

Have you attempted to ‘Generate Visual Studio Project Files’? You can do this one of two ways. You can navigate to the project file itself on your hard drive and right click the file. This’ll give you the option to Generate Project Files. You can also do this from the editor by clicking File > Generate Project Files. This normally fixes any problems caused by newly added code files that were originally foreign to the project.

Hope this helps!

Hi ,
I already have the Visual Studio Project Files. The problem is, I can’t simply add the code to the project. First, it is a whole bunch of files in lot of different folders and second, the project I created should be reproducible.
That means, the repository I am using is shared and updated by other people, too, so this should be as dynamically as possible.

That’s why I am asking if there is a possibility to simply give the links to the .cpp files, and UBT uses them. It already works with the Header files, so I hoped it would work with the .cpp files, too.

Or maybe there is a possibility to tell UBT not only to look for .cpp files in the project folder, which does it by default, but in folders, that the user can configure.

I hope I could make myself a little clearer.
But even if the question isn’t solved yet, thanks in advance :wink:

Greetings,
Dominik

The way I’m understanding what you want to try to do is that you want to use source control so that you can easily sync with other programmers and keep everything clean. I would suggest looking into source control methods such as Perforce. I do not believe (I could be wrong) that UE4 has source control of it’s own.

I hope you can find the solution to your issue.

There is already some sort of source control. The problem is to get the source files from the repository included into the building process.
The source files originally weren’t created for games but for other projects, so I want to avoid copying the files around, which would be the last (simple, but rather ugly) solution.
And like I said before, it works absolutely fine with the Header files so I would be surprised if there wasn’t a way to include the .cpp files as well.

I haven’t made much progress so far but I wanted to leave a comment to let you know that I am still looking into how you could include the .cpp files from outside of the project. I’ll try to get back to you as soon as possible with a solution.

I asked a similar question a few months back and the answer seemed to be that UE4 really isn’t flexible with it’s project source structure. In the end I settled on using a git submodule so I basically had a local clone of the external repo inside my UE4 project Source directory tree.

That said, I had control over the directory structure of the repo. If you don’t, you may still have an issue - I don’t know if having the cpp files within the UE4 project directory tree will be sufficient for UBT, or if it actually requires them to all be in a directory named ‘Private’.

From looking into this, it seems like you shouldn’t need to include the .cpp files themselves if you link the files as a library. I’m not very familiar with libraries myself and I’m not sure how much they differ from your current method, if at all, but it may be something to look into. If you need information about linking libraries, you can find this at the following link:

Nice to hear, that you have been still after this :slight_smile:
I used to include the files as static libraries, but the code is supposed to run cross-platform, including Android and iOS and with that, libraries aren’t the best solution.

Just to let you know:
My current workaround is having a little script in in the Build.cs which searches for the necessary files and copies it into the include folder in my project directory and add the folder to the include paths using Public/PrivateIncludePaths.Add().
One problem is, that the main header file of the project (i.e. ProjectName.h) has to be included as the first include in all of the .cpp files in the project folder, but this is done rather quick in my C# script.

The problem I have now is a huge amount of compiling errors, but these seem to be caused by the code itself or rather a broken header file.

Thanks for your help !

It seems like your workaround would be the best solution to this issue, as there doesn’t seem to be an actual solution. I will continue looking into this as I have and asking around, but I’m going to leave your workaround as the top answer for the moment.

Have you fixed the compiling error, @zapfdk? I uses a similar approach to yours (but I include all .cpp files in PCH header file after including the .h files in correct order, and wrap them inside a #ifndef #endif preprocessor), but when I compile it, I have double definition error.