Announcement

Collapse
No announcement yet.

Using Clang to compile on Windows

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Using Clang to compile on Windows

    At Epic we've been working on adding support for the latest Clang compiler on Windows platform. Some version of Clang is already used on most other platforms, but getting Clang running on Windows makes it easy to use some of Clang's excellent diagnostic features, and perhaps eventually to allow you to compile game modules without requiring Visual C++ to be installed. Of course, we'll always continue to support Visual C++!

    So the Clang team has been making great improvements over the last couple of months on Windows support. It's actually getting pretty close to parity with Visual C++, though quite a few bugs remain.

    I thought I'd share the current state of compiling UE4 using Clang on Windows so far with you guys! If you grab the latest code from GitHub master branch (as of 12/19/2014), you can try this out if you're interested!

    To use Clang on Windows:
    1. Install the latest Clang compiler (click 'Windows Installer'.)
    2. Open UEBuildWindows.cs. Set bCompileWithClang=true.
    3. Rebuild!



    Cool stuff:
    1. You can now use Clang to compile, link and run most small UE4 programs (SlateViewer, UnrealHeaderTool, UFE, etc) on Windows.
    2. You can now use Visual Studio to debug Clang-compiled code, as long as you use the Visual Studio linker (the default setting).
    3. Clang now has full compatibility with Microsoft's C++ object format. This means it can seamlessly link with code compiled with VC++, such as third party libraries.



    Bad stuff:
    1. The full UE4 editor doesn't compile yet, mainly due to some parsing issues with DirectX header files and also some issues with system include paths (XAudio). We should hopefully be able to fix this.
    2. Precompiled headers aren't supported when compiling with Clang on Windows yet. There are several bugs in the Clang compiler preventing this. This means compile times are currently very slow!
    3. We still require and use the Windows "resource tool" (rc.exe) and lib tool (lib.exe), even when compiling and linking using Clang.
    4. We still require the Windows SDK headers and libraries. This means you must have the Windows SDK or Visual Studio installed in order to build with Clang. I don't yet see any reasonable way to avoid this when compiling applications that use the standard C library, or anything that links against UE4 code.
    5. If you use the Clang linker (LLD), you won't be able to debug using Visual Studio yet (no PDB support.)
    6. IncrediBuild (XGE) occasionally has troubles when compiling using Clang on Windows. So you might want to disable XGE if you see strange errors.



    Tweakables:
    • UEBuildWindows.bCompileWithClang - When enabled, compiles code using the Clang compiler. The Visual C++ compiler will not be used at all, but by default we'll link with the VC++ linker (which supports debug symbols)
    • UEBuildWindows.bAllowClangLinker - Turns on the Clang linker (LLD.exe). You can now link UE4 programs with this, but you won't be able to debug in Visual Studio.
    • UEBuildWindows.bUseVCCompilerArgs - By default we use 'clang-cl' to invoke the Clang compiler, which is a shallow wrapper around Clang that handles Visual C++ command-line arguments. If you switch this off, we'll instead invoke Clang.exe directly using normal GCC-style arguments.


    Note: We now support both 'clang-cl' (the Clang wrapper binary that supports VC-like command-line parameters) as well as invoking Clang directly 'clang.exe' using native GCC-like parameters. I wanted us to support both for now, as each method has different quirks and limitations.

    If anyone is interested in this stuff and would like to help out with improving support for Clang on Windows, please chime in or send me a message. It's a fun and interesting side project that I'm excited about, as there are potential cool powers this could enable in the future. For example, in the future we'd like to replace our naive C++ parsing in Unreal Header Tool with the robust Clang parser, then use the AST to harvest reflection meta-data instead of scraping the code directly.


    --Mike

    #2
    This is pretty cool. I am going to play around with this on the weekend.
    Quinton Delpeche
    Founder - Gobbo Games | Designer - The Colony 2174 (Board Game) | Developer - Riders of Asgard

    Comment


      #3
      I have been planning to look at this, anyway. I've been running with MSys2 which gives you the pacman package manager on Windows and makes maintaining an up to date clang-based toolchain much easier.

      Comment


        #4
        I've actually been wanting to know if anything would happen on this front since I saw the Visual Studio 2015 support for Clang.

        Great news! Both that Epic are looking into Clang and that it's been improving on Windows.

        Comment


          #5
          Fantastic news!!!
          Can't really wait to try it! Clang is such a powerful and beautifully designed compiler. So glad to stumped upon this thread!

          Great, great job! Thanks!!!

          Comment


            #6
            Hurray, for more meaningful errors with clang!

            That's awesome thanks. Going to try it out.
            https://github.com/iniside/ActionRPGGame - Action RPG Starter kit. Work in Progress. You can use it in whatever way you wish.

            Comment


              #7
              @Mike Fricker
              Hi Mike, I've just wondered if there is any update on clang on windows subject.

              Thank you.

              Comment


                #8
                @smallB: I found a message of Mike Fricker on this when I searched for something related

                https://llvm.org/bugs/show_bug.cgi?id=23071

                So it appears according to that post the Editor can be build with LLVM clang r231657, for which a windows build is available from llvm.

                I didn't yet try it but it would be awesome to have clang as a real option against the MSC++ compiler and the mingw variants.

                What is a real pitty that there's no C and std c++ library apart from Microsofts, waiting for a Microsoft compiler that does everything according to current standards we'll probably have a new standard already again.

                Comment


                  #9
                  Originally posted by Mikand79 View Post
                  @smallB: I found a message of Mike Fricker on this when I searched for something related

                  https://llvm.org/bugs/show_bug.cgi?id=23071

                  So it appears according to that post the Editor can be build with LLVM clang r231657, for which a windows build is available from llvm.

                  I didn't yet try it but it would be awesome to have clang as a real option against the MSC++ compiler and the mingw variants.

                  What is a real pitty that there's no C and std c++ library apart from Microsofts, waiting for a Microsoft compiler that does everything according to current standards we'll probably have a new standard already again.
                  First of all, thank you for your answer.
                  Second of all, couldn't agree more with you.

                  Thanks and best regards.

                  Comment


                    #10
                    Update on this.

                    As of some changes yesterday in master branch, you can compile and run the whole Unreal Editor using Clang on Windows platform.

                    It still has a way to go, but many of the problems mentioned above are solved (PCHs now work, basic debugging works, all of the Windows and DirectX headers are digestable, etc.) Getting UE4 working on Clang for Windows has been a background project of mine for almost a year. If anyone in the community is interested in jumping in to help improve this new workflow, we'd love that!


                    To test Clang on Windows (beta!!):

                    - Get latest UE4 master branch (4.9)

                    - Install LLVM+Clang for Windows version r231657. It's a quick install, you don't need to compile anything yourself.

                    - Open UEBuildWindows.cs, and change bCompileWithClang to true.

                    - Switch to Debug Editor configuration. (See below for more info.)

                    - Rebuild the game or engine


                    Notes:

                    - You must use Clang r231657 (3.7.0 alpha). There are no other releases that I've found that are able to compile UE4 successfully. Most of the newer releases crash during compiling, and older releases aren't able to digest Windows header files well enough.

                    - Compiling in Development currently will not work due to bugs with Clangs handling of inlining and DLL exports. Monolithic builds should work, though.

                    - Occasionally you may see runtime crashes (stack overflows or access violations) when running UE4 compiled with Clang on Windows. These may very well be bugs in Clang (remember, we're using an alpha release of Clang 3.7.0.) Unfortunately without better debugger support, it can be difficult to workaround these

                    - When debugging, you will only have functions and line numbers, no variables or parameters. Clang only has preliminary support for Microsoft's PDB format currently.

                    - XGE (IncrediBuild) is fully supported with Clang on Windows.

                    - Shared PCHs aren't supported yet (similar to on Mac and Linux), so iterative compile times may be slower compared to Visual C++. However the Clang compiler is actually quite fast for rebuilds, probably because of missing debug info though.

                    - There may be some compiler warnings that are different between Mac/Linux and Windows. We haven't done a line-by-line comparison yet.

                    - Build products and executables for Clang and VC++ overlap on disk. So you have to rebuild to switch back and forth. Sorry.

                    - Clang on Windows has a few bugs that we've had to work around. You might see some strange things from time to time. Every new release of Clang has different behavior on Windows.

                    - We still use the VC++ Linker, even when compiling using Clang. The new Clang linker (lld.exe) can be enabled with bAllowClangLinker but it will crash when linking some modules. It also doesn't produce usable debug symbols by Visual Studio (yet).

                    Comment


                      #11
                      Does that mean we can compile C++ code on windows without visual studio? Can we compile just with Clang and use something else to edit the C++ files?
                      UDK and UE4 programmer and Unreal engine 4 betatester. Currently working on commercial VR games for PSVR.
                      Deep knowlegde of C++ and blueprints. Open to freelance work.
                      Games released, Deathwave(Steam), VRMultigames(Steam), DWVR(Steam,Oculus,PSVR):
                      http://store.steampowered.com/app/463870
                      http://store.steampowered.com/app/500360
                      http://store.steampowered.com/app/520750

                      Comment


                        #12
                        Almost there, as Mike Fricker said, you still need the Windows SDK tools/headers/libs and the VC linker, and the linker isn't part of the SDK anymore since Windows 8; and the output isn't perfect yet. That means you still need VS 2013.

                        Comment


                          #13
                          Would be possible to use the windows SDL/headers/lib and linker of VS2012? Im teaching a ue4 programming course in my university, and they cant install VS2013, but have VS2012 installed. Ive been looking at using linux as an alternative.
                          UDK and UE4 programmer and Unreal engine 4 betatester. Currently working on commercial VR games for PSVR.
                          Deep knowlegde of C++ and blueprints. Open to freelance work.
                          Games released, Deathwave(Steam), VRMultigames(Steam), DWVR(Steam,Oculus,PSVR):
                          http://store.steampowered.com/app/463870
                          http://store.steampowered.com/app/500360
                          http://store.steampowered.com/app/520750

                          Comment


                            #14
                            Right. Many bits of Windows-specific engine code will obviously always depend on the Windows platform SDK header files and runtime libraries. But I'm hoping that in the not-so-distant-future, we can support compiling game modules and engine plugins without requiring the Windows SDK files. We can certainly make our Core headers conditionally not require the Windows headers, but there is a bit of refactoring to do there. And even if we're successful, we'd still need some of the import libraries from Visual C++, because I don't see us supporting MingGW. We'll see. This is just the very first steps. Our goal eventually is to allow people to compile game modules without any additional prerequisites beyond what we ship included with UE4 on either GitHub or through the Launcher. It is definitely a long term thing.

                            Comment


                              #15
                              @Mike Fricker

                              Strange sidebar here. I'm using Visual Studio while learning the ins and outs of the engine because intellisense is useful for learning. Normally, I'm a vim user and although vsVim for visual studio is usable it's not as smooth or efficient as proper gVim with plugins and my scripts. Great, what does this have to do with windows clang UE4? Well Vim does have great C-family 'intellisense' when paired with a plugin called YouCompleteMe. YCM uses libclang on the backend. For this plugin you can write a python file that specifies the compiler flags and things like include directories, but that seems like a lot of directories for UE4, and more importantly I'm afraid it is brittle as the engine changes or I bring different plugins in.

                              As an alternative to this python file you can instead just feed YouCompleteMe a compile_commands.json file, something usually spit out from CMake or BEAR for non-Cmake projects. Now this feature is in a bit of a strange spot on windows anyway as it seems to only support Make and Ninja style projects, not visual studio solutions at the moment. So my question to you is: despite using clang for the actual compilation is this process still using the visual studio toolchain for the project or is it something closer to make/cmake on unix?
                              VR R&D @Shopify

                              Comment

                              Working...
                              X