Search code examples
c++windowsunreal-engine5

How to create a C++ project in UE5 without installing Visual Studio?


I would like to use Visual Studio Code (or any other IDE) and gcc. I have g++ installed:

g++ -v
Using built-in specs.
...
gcc version 10.3.0 (tdm64-1)

However in UE5, when I try to create a new C++ project, an error is shown:

No compiler was found. In order to use a C++ template, you must first install Visual Studio 2019

The "must" part doesn't sound very serious to me: Visual Studio seems to be 8GB and I'm not even sure whether it's free.

Is there a way to use C++ in UE5, without installing Visual Studio?

Note: as also proposed in some comments, there is a guide to setup VS Code: https://docs.unrealengine.com/5.0/en-US/setting-up-visual-studio-code-for-unreal-engine/. However, the problem with that guide is that: "This guide assumes that you have installed Unreal Engine and created a C++ project with it.", but you can't create a project without Visual Studio, because the create button is disabled.


Solution

  • A C++ project for Unreal engine can be created manually, since it's defined exclusively in text files arranged in a specific directory structure in the file system.

    Firstly, you need the project file itself. It is a json file with .uproject extension (let's call it MyProject for example). The contents required are a EngineAssociation field for the engine version (which can be both be intalled from Epic Games Launcher or built from source), and at least one module in the Modules array. Here is an example:

    {
        "FileVersion": 3,
        "EngineAssociation": "4.27",
        "Category": "",
        "Description": "",
        "Modules": [
            {
                "Name": "MyProjectModule",
                "Type": "Runtime",
                "LoadingPhase": "Default",
                "AdditionalDependencies": [
                    "CoreUObject",
                    "UMG",
                    "Engine"
                ]
            }
        ]
    }
    

    Next, we need to create target files. Create a Source folder in the same directory as the uproject. In that folder, two files are needed: one for the standalone project and one for the in-Editor project. They are named (ProjectName).target.cs and (ProjectName)Editor.target.cs (note: no space or underscore before Editor, i.e. MyProject.target.cs and MyProjectEditor.target.cs).

    These files are used to set up overall build settings. Contents can be nearly identical. In their simplest form, here is .target.cs:

    using UnrealBuildTool;
    using System.Collections.Generic;
    
    public class MyProjectTarget : TargetRules
    {
        public MyProjectTarget( TargetInfo Target) : base(Target)
        {
            Type = TargetType.Game;
            DefaultBuildSettings = BuildSettingsVersion.V2;
        }
    }
    
    

    and Editor.target.cs:

    using UnrealBuildTool;
    using System.Collections.Generic;
    
    public class MyProjectEditorTarget : TargetRules
    {
        public MyProjectEditorTarget( TargetInfo Target) : base(Target)
        {
            Type = TargetType.Editor;
            DefaultBuildSettings = BuildSettingsVersion.V2;
        }
    }
    
    

    Finally, you need to define the MyProjectModule module that was declared in the uproject. Make and a subfolder named after the module in the Source folder (for this example, Source/MyProjectModule). There, three files are needed: a build.cs which defines how to build this module, a ModuleName.h and ModuleName.cpp files that "implement" the module for Unreal's reflection system.

    Simplest Build.cs:

    using UnrealBuildTool;
    
    
    public class MyProjectModule : ModuleRules
    {
        public MyProjectModule(ReadOnlyTargetRules Target) : base(Target)
        {
            PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
                
            PublicDependencyModuleNames.AddRange(new string[] { 
                "Core", 
                "CoreUObject", 
                "Engine"
            });
        }
    }
    

    Simplest module .h:

    #pragma once
    
    #include "CoreMinimal.h"
    

    Simplest module .cpp:

    // Copyright Epic Games, Inc. All Rights Reserved.
    
    #include "MyProjectModule.h"
    #include "Modules/ModuleManager.h"
    
    IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, MyProjectModule, "MyProjectModule" );
    

    This should be a sufficient base project to follow the rest of the official guide for using VS Code / other IDE. That doesn't answer the part of your question about using g++ for compilation, and will require either MSVC on Windows or Clang on Linux and Mac. I hope that's not a big issue.