How to make Unreal Engine work correctly with CLion?
Unreal Engine by Epic Games is a masterpiece. THE Game Engine. It is super powerful and convenient to use.
There is a problem, though.
Epic Games developers use Windows and Visual Studio to develop the engine and don’t invest the same amount of time to other development tools.
Unreal Engine supports the following source code editors:
-
Xcode
-
Visual Studio
-
Visual Studio Code
-
CLion
Visual Studio and Visual Studio Code are the first-class citizens and work well with UE4. More importantly, Visual Studio Code is cross-platform so you can just use that. It is okay text editor.
Xcode and CLion on the other hand are next to impossible to use with the project files generated by UE4.
JetBrains are good in making IDEs and CLion is a perfect C++ IDE. THE perfect engine needs THE perfect IDE so let’s marry them.
The problem
The current version of UE4 (4.26) generates incorrect CMake project files. The bug exists for a long time but has not been fixed yet in any official build of the engine.
The problem with the files it generates is that they don’t have a few important project-wide macro definitions.
For example, each project has a ${PROJECT_NAME}_API
macro.
The macro is used for exporting/importing C++ functions and classes, which is required on Windows, where it expands to __declspec(dllimport)
or __declspec(dllexport)
storage-class attributes.
The symbol is not defined which makes C++ definitions incorrect and CLion yells at you about syntatic errors in the code. This also breaks autocomplete.
Other examples are GENERATED_BODY
, UCLASS
, UPROPERTY
and many more U4 macros which are not properly defined for CMake-based projects because some of the symbols they depend on are not defined either.
The solution
If you search the Internet hard enough, you can find a patch that fixes the problem.
So go and grab the patch.
Then open your terminal, go to the engine source code (for macOS it should be /Users/Shared/Epic Games/UE_4.26
).
Then apply the patch:
$ patch -p1 < /path/to/ue4_25-defines-fix.patch
Now, if you use zsh
as I do, switch to bash
temporarely and source a setup environment script for your platform, in my case macOS:
$ bash
bash-3.2$ source Engine/Build/BatchFiles/Mac/SetupEnvironment.sh -mono Engine/Build/BatchFiles/Mac
The next step is to build and install Unreal Build Tool:
bash-3.2$ xbuild Engine/Source/Programs/UnrealBuildTool/UnrealBuildTool.csproj
The command will spit a deprecation warning on you, then compile the tool and install it to the right place replacing the old one so that the Unreal Editor can pick it up.
Now you can generate CLion project and open the project in the IDE.
It will take some time for it to load all symbols, though.
Select ${PROJECT_NAME}Editor|Debug
build target and build it each time you want to make your code changes available to the Unreal Editor.
What about other IDEs and editors that support CMake?
In this post I described how to generate correct CMake files. Once you have the files, you can import them into any IDE/editor that supports CMake and everything should work out of the box.
For example, here I have a project opened in Emacs:
Symbol navigation, autocomplete, documentation browser, and refactor work perfectly well.
Happy coding! :-)