Use c++ classes from Unreal project in another c++ project?

Specifically, I would like to reference game project classes in a separate unit testing project. I am using the CATCH unit testing framework rather than the Unreal automation, and I am hoping to keep the game and tests as separate projects for separation of interests. However, I cannot successfully link the game project to the unit testing one.

I can set the include directories to include the headers from the game in the unit testing project. But I cannot get the latter project to find implementation files. I have tried linking the libraries I could find, but I always get the LNK2001 unresolved external symbol error. So I am stuck with undefined methods.

I am new to linking between projects altogether, so likely I am missing something simple. I am using Visual Studio Community 2013. The most likely looking libraries I found are in Intermediate\Build\Win64\Editor**.lib. I included these library directories in (unit testing) Project Properties → Linker → General → Additional Library Directories, and I included the library filenames in Project Properties → LInker → Input → Additional Dependencies.

Are these the right libraries?

Does Unreal output libraries that can be used by other projects?

Is there any way to reference classes in my game project in another project?

Should I make three projects: one for linking with Unreal, one (which outputs a library rather than executable) with the source code, and one with the test code?

I not fully understand what are you trying to do with that speration, but basing from fact you trying to wiggle around VS configs, you seem to lack knowlage how UE4 build system works. UE4 has it’s own build system called Unreal Build Tool (UBT), it does not require VS IDE to function, it just need VS compiler to compile the code. It operates based on C# build scripts which you can find in Source directory and all configuration is done here. Because of that VS project files has 0 impotence for UBT, it can build without them, they exist just for VS IDE convenience.

Also UE4 code structure is kind of similar to Linux Kernel, it’s divided in to modules which can be un/loaded dynamically, when you make C++ project you create new engine module which will contain game code, so your game code is technically extension of engine. Keeping this in mind it might be hard to link it to something other then UE4, besides in order to properly run it and test it you need the whole UE4 running either way due to it’s object management system.

Now i’m not sure how you seperate projects, because i’m not sure if you mean UE4 project (uproject) or VS project, but best way to divide “interest” in UE4 is to create separate UE4 module which builds only when you do “Development” or “Debug” or even “Testing” builds (you can set it up that way in build scripts). So i think incorporating CATCH as a sperete UE4 module will be the best approach.

Here docs about UBT:

And here tutorial how to link external library with build scripts which might be useful for you:

Thank you for your quick response, and sorry for my delayed one. You are right; I didn’t realize that Unreal’s build system ignores most of VS’s configs. I have been looking into the system more explicitly, and I hope you don’t mind if I ask for some specific pointers.

To clarify, I was trying to make a separate VS project that would test my custom code for the Unreal project. I have constructed my code to be fairly insulated from Unreal, wrappers and such. So I was hoping to link up to that code, make the appropriate mocks for testing, and run the relatively simple Catch application from command line.

I understand now that this may not be as simple as hoped. I see three possibilities right now.

Module: As you say, I could make a separate module in the VS project, and this would of course be able to link to my code. I would gladly make an editor module, but it would be unfortunate if, as you say, I had to have the editor running to test my mostly editor-free code. In addition, I am not confident I would be able to run Catch since it requires a main().

Program: The programs in the source code engine look promising (e.g. BlankProgram), as they run more or less how I would want Catch to run, but I am not confident that I can link any of my code to a separate program. This seems like a robust solution IF it is a solution.

Project with shared source: It might be possible to have a directory that is shared between the two projects with only the editor-free code. I could manually update each project when I add code that I want to test. A structure something like

GameCode → WrapperCode & SourceCode

TestCode → TestingCode & SourceCode

However, first I am not sure that there might be something nefarious going on behind the scenes, and second I don’t know if I would be able to access the Unreal classes that seemed to simple to wrap, e.g. FVector.

Thanks for your help. You’ve certainly solved the original, misled question, but I hope you can help with the extra.

I never used catch, dont know how it works and how implement it best. Also FVector is struct, i more worried with UObject classes which is spawning object in unstandard way

Ah, you’ve given me hope! If the structs work I should be golden. I’ve wrapped away from the UObjects, as they were getting on my nerves anyway, and I can easily mock those. Of course they could still cause problems… fingers crossed?

Catch is simple to integrate, just add the given header file and put some junk in main(). My dearest hope now is that I can implement it as a Program, or Tool, or whatever the projects in the Programs solution folder are called. Impressively little documentation on that, mostly consisting of “duplicate BlankProgram”. So that’s what I’m trying. :slight_smile:

I’ll report back, works or not.

Alright, it’s looking good so far. I do want to share a couple of things, because the hacks are hilarious.

I put the Target, Build, etc. in a directory Programs under my game’s Source. Had Unreal regenerate my project and the NewProgram shows up in VS. Splendid!

I added catch.hpp to the directory and included it in NewProgram.cpp. Immediately, there is an error about DWORD. Small bit of googling and I find that catch has some Windows includes and any such includes should be wrapped:

#include "AllowWindowsPlatformTypes.h"
#include "MyWindowsStuff.h"
#include "HideWindowsPlatformTypes.h"

:slight_smile: Fantastic, it works like a charm. Now a new error, “struct ‘std::_Tree_node’ can never be instantiated”. Between some talk of this error being a Visual C++ bug and a forum post about having to disable the error, I decided that it perhaps needs to be disabled. This is of course a much less comfortable hack, but we’ll see if it causes problems.

Now my biggest problem is that the Program’s main supplies an int ArgC and a TCHAR* ArgV[], while Catch needs an int ArgC and a char * const ArgV[]. I am terrible with this unicode stuff, and I don’t know what I should be doing here to convert Unreal’s TCHAR. TCHAR_TO_ANSI can do the conversion as long as they are not arrays. Should I be using StringCast somehow, or looping over some array?

I made a forum post of a walkthrough of the way I settled on for using the third party Catch unit testing with Unreal, using modules as answered below…

Sorry , I hadn’t realized I didn’t accept your answer after you first gave this. Fixed.