microsoft / DirectXTK

The DirectX Tool Kit (aka DirectXTK) is a collection of helper classes for writing DirectX 11.x code in C++
https://walbourn.github.io/directxtk/
MIT License
2.57k stars 511 forks source link

Can't compile DirectXTK with Clang-cl #96

Closed LepelTsmok closed 7 years ago

LepelTsmok commented 7 years ago

Hello DirectXTK-Team,

I'm a huge fan of your work and of the clang compiler. So I decided to make a project with both of them. I had compiled the DirectXTK library with the Visual Studio 2015 compiler and used it for my project with clang-cl. The result wasn't satisfying, because my program wasn't able to display the texture which I successfully loaded with DirectXTK. Compiling the code with the microsoft compiler showed the texture. So I wanted to compile the DirectXTK library with clang-cl and the new DirectXMath library. But it failed, with the following errors:

1>------ Erstellen gestartet: Projekt: DirectXTK_Desktop_2015, Konfiguration: Release x64 ------ 1>clang-cl.exe : warning : support for '/Yc' without a corresponding /FI flag not implemented yet; flag ignored [-Wclang-cl-pch] 1>clang-cl.exe : warning : argument unused during compilation: '/Gm-' [-Wunused-command-line-argument] 1>clang-cl.exe : warning : argument unused during compilation: '/FpBin\Desktop_2015\x64\Release\DirectXTK.pch' [-Wunused-command-line-argument] 1>clang-cl.exe : warning : support for '/Yu' without a corresponding /FI flag not implemented yet; flag ignored [-Wclang-cl-pch] 1>clang-cl.exe : warning : argument unused during compilation: '/Gm-' [-Wunused-command-line-argument] 1>clang-cl.exe : warning : argument unused during compilation: '/FpBin\Desktop_2015\x64\Release\DirectXTK.pch' [-Wunused-command-line-argument] 1> In file included from Src\AlphaTestEffect.cpp:15: 1> In file included from Src/EffectCommon.h:19: 1>Src/PlatformHelpers.h(77,13): error : expected unqualified-id 1> typedef public std::unique_ptr<void, handle_closer> ScopedHandle; 1> ^ 1>Src\AlphaTestEffect.cpp(90,57): error : template specialization requires 'template<>' 1> const ShaderBytecode EffectBase::VertexShaderBytecode[] = 1> ~~~~~~~~~ ^ 1> template<> 1>Src\AlphaTestEffect.cpp(99,46): error : template specialization requires 'template<>' 1> const int EffectBase::VertexShaderIndices[] = 1> ~~~~~~~~~ ^ 1> template<> 1>Src\AlphaTestEffect.cpp(113,57): error : template specialization requires 'template<>' 1> const ShaderBytecode EffectBase::PixelShaderBytecode[] = 1> ~~~~~~~~~ ^ 1> template<> 1>Src\AlphaTestEffect.cpp(122,46): error : template specialization requires 'template<>' 1> const int EffectBase::PixelShaderIndices[] = 1> ~~~~~~~~~ ^ 1> template<> 1>Src\AlphaTestEffect.cpp(137,122): error : template specialization requires 'template<>' 1> SharedResourcePool<ID3D11Device, EffectBase::DeviceResources> EffectBase::deviceResourcesPool; 1> ~~~~~~~~~ ^ 1> template<> 1>Src\AlphaTestEffect.cpp(201,53): warning : suggest braces around initialization of subobject [-Wmissing-braces] 1> static const XMVECTORF32 selectIfTrue = { 1, -1 }; 1> ^~~~~ 1> { } 1>Src\AlphaTestEffect.cpp(201,53): warning : suggest braces around initialization of subobject [-Wmissing-braces] 1> static const XMVECTORF32 selectIfTrue = { 1, -1 }; 1> ^~~~~ 1> { } 1>Src\AlphaTestEffect.cpp(202,52): warning : suggest braces around initialization of subobject [-Wmissing-braces] 1> static const XMVECTORF32 selectIfFalse = { -1, 1 }; 1> ^~ 1> { } 1>Src\AlphaTestEffect.cpp(202,52): warning : suggest braces around initialization of subobject [-Wmissing-braces] 1> static const XMVECTORF32 selectIfFalse = { -1, 1 }; 1> ^~ 1> { } 1>Src\AlphaTestEffect.cpp(203,52): warning : suggest braces around initialization of subobject [-Wmissing-braces] 1> static const XMVECTORF32 selectNever = { -1, -1 }; 1> ^~ 1> { } 1>Src\AlphaTestEffect.cpp(203,52): warning : suggest braces around initialization of subobject [-Wmissing-braces] 1> static const XMVECTORF32 selectNever = { -1, -1 }; 1> ^~ 1> { } 1>Src\AlphaTestEffect.cpp(204,53): warning : suggest braces around initialization of subobject [-Wmissing-braces] 1> static const XMVECTORF32 selectAlways = { 1, 1 }; 1> ^~~~~ 1> { } 1>Src\AlphaTestEffect.cpp(204,53): warning : suggest braces around initialization of subobject [-Wmissing-braces] 1> static const XMVECTORF32 selectAlways = { 1, 1 }; 1> ^~~~~ 1> { } 1> In file included from Src\AlphaTestEffect.cpp:15: 1>Src/EffectCommon.h(154,13): warning : field 'mDeviceResources' will be initialized after field 'constants' [-Wreorder] 1> mDeviceResources(deviceResourcesPool.DemandCreate(device)), 1> ^ 1> Src\AlphaTestEffect.cpp(142,5): note: in instantiation of member function 'DirectX::EffectBase::EffectBase' requested here 1> : EffectBase(device), 1> ^ 1> In file included from Src\AlphaTestEffect.cpp:15: 1> In file included from Src/EffectCommon.h:21: 1>Src/SharedResourcePool.h(82,17): warning : field 'mResourceMap' will be initialized after base 'DirectX::EffectBase::DeviceResources' [-Wreorder] 1> mResourceMap(resourceMap), 1> ^ 1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\memory(901,29): note: in instantiation of member function 'DirectX::SharedResourcePool<ID3D11Device , DirectX::EffectBase::DeviceResources>::WrappedData::WrappedData' requested here 1> ::new ((void )&_Storage) _Ty(_STD forward<_Types>(_Args)...); 1> ^ 1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\memory(971,7): note: in instantiation of function template specialization 'std::_Ref_count_obj<DirectX::SharedResourcePool<ID3D11Device , DirectX::EffectBase::DeviceResources>::WrappedData>::_Ref_count_obj<ID3D11Device &, std::shared_ptr<DirectX::SharedResourcePool<ID3D11Device , DirectX::EffectBase::DeviceResources>::ResourceMap> &>' requested here 1> new _Ref_count_obj<_Ty>(_STD forward<_Types>(_Args)...); 1> ^ 1> Src/SharedResourcePool.h(58,34): note: in instantiation of function template specialization 'std::make_shared<DirectX::SharedResourcePool<ID3D11Device , DirectX::EffectBase::DeviceResources>::WrappedData, ID3D11Device &, std::shared_ptr<DirectX::SharedResourcePool<ID3D11Device , DirectX::EffectBase::DeviceResources>::ResourceMap> &>' requested here 1> auto newValue = std::make_shared(key, mResourceMap, args...); 1> ^ 1> Src/EffectCommon.h(154,50): note: in instantiation of member function 'DirectX::SharedResourcePool<ID3D11Device , DirectX::EffectBase::DeviceResources>::DemandCreate' requested here 1> mDeviceResources(deviceResourcesPool.DemandCreate(device)), 1> ^ 1> Src\AlphaTestEffect.cpp(142,5): note: in instantiation of member function 'DirectX::EffectBase::EffectBase' requested here 1> : EffectBase(device), 1> ^ 1> 10 warnings and 6 errors generated. ========== Erstellen: 0 erfolgreich, 1 fehlerhaft, 0 aktuell, 0 übersprungen ==========

Kind regards LepelTsmok

walbourn commented 7 years ago

DirectXTK is not likely to build fully with clang, even using the Microsoft codegen clang. In particular, any place I make use of C++/CX extensions (/ZW) will not work as they are unsupported by clang (some versions of the input classes and the audio implementation).

It should be possible to build portions of the library, but it's likely to take some effort to work through it all. I do use VS 2017's /permissive- to make the code as portable as possible, but it's obviously not catching everything.

You also should create a fresh clang static library VC++ project instead of trying to use one of the existing vcxproj files. A number of those warnings are because the Microsoft clang-cl does not support all the options that Visual C++ cl does.

Trying to build with clang, I see:

walbourn commented 7 years ago

How did you get past the problems with wrl.h not working with clang? At a minimum, you need wrl\client.h to define Microsoft::WRL::ComPtr.

I should probably do a cleanup pass for DirectXTK, DirectXTK12, DirectXTex, and DirectXMesh trying to use clang. Remember that Microsoft clang-cl is experimental. so I'm not sure how good the resulting codegen is here, but it does find conformance bugs missed by /permissive-. It's also a problem that the winrt include folder in the Windows SDK doesn't really work with clang.

In short, I don't think you can build DirectXTK with clang because the Windows SDK itself doesn't support it, at least not with the current Windows SDKs.

walbourn commented 7 years ago

I've run through DirectXTK, DirectXTK12, DirectXTex, and DirectXMesh trying to build them with clang. The latest DirectXMath lets you get that part building, but neither the Windows 8.1 SDK or Windows 10 SDK (15063) really supports building with clang: Direct3D 11 headers, Direct3D 12 headers, the D3DX12 utility header, and the Windows Runtime Library (WRL) don't build with clang. This might be fixed in some future release, but it's not there yet.

I did take the opportunity to clean up a number of the issues in my source, but not all of them. Notably I don't support the _XM_NO_XMVECTOR_OVERLOADS_ scenario in these libraries. I could, but it makes the code a lot messier and right now that doesn't really buy you anything.

Really the Microsoft clang support is intended for building libraries (static libs or DLLs) that are hosted in a larger Visual C++ built program. As such, your Direct3D 11 program should be built using Visual C++ and not clang, linking with DirectXTK built with Visual C++, and perhaps linked to other code (not Windows platform specific) built with Microsoft clang.

If a future release of the Windows 10 SDK supports clang for Win32 development more fully, I can try again and resolve the outstanding issues. In the meantime, DirectXMath itself is just well ahead of the rest of the Windows 10 SDK in supporting clang.