Search code examples
c++windowscmakecompiler-errorsdirectx

DirectX CMake includes break Windows.h


When i try to include the DirectX SDK directory into my Project using CMake, the header files in windows.h throw errors when i try to #include <windows.h> (no directx header are included before).

commands i use to generate/compile:

cmake -S . -B build -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug
cd build
nmake

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

project( NisPaper )

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
IF( EXISTS "${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json" )
   EXECUTE_PROCESS( COMMAND ${CMAKE_COMMAND} -E copy_if_different
   ${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json
   ${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json
)
ENDIF()

ADD_LIBRARY(NisPaperLibs
    gdimanager.cpp
    gdirenderer.cpp
    d3dmanager.cpp
    winmanager.cpp
    d3drenderer.cpp
)

add_executable( NisPaper main.cpp )
target_link_libraries(NisPaper NisPaperLibs)

target_link_libraries(NisPaper gdiplus.lib)
target_link_libraries(NisPaper gdi32.lib)
target_link_libraries(NisPaper user32.lib)
target_link_libraries(NisPaper Dwmapi.lib)

find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
target_link_libraries( NisPaper ${OpenCV_LIBS} )

#This line causes the errors
include_directories("C:/Program Files (x86)/Microsoft DirectX SDK (March 2008)/Include")

errors:

C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidlbase.h(6184): error C2061: syntax error: identifier '__RPC__out_xcount_part'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidlbase.h(6188): error C2061: syntax error: identifier '__RPC__in_xcount_full'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidlbase.h(6392): error C2061: syntax error: identifier '__RPC__out_xcount_part'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidlbase.h(6396): error C2061: syntax error: identifier '__RPC__in_xcount_full'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidlbase.h(6600): error C2061: syntax error: identifier '__RPC__out_xcount_part'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidlbase.h(6604): error C2061: syntax error: identifier '__RPC__in_xcount_full'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidl.h(14536): error C2061: syntax error: identifier '__RPC__inout_xcount'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidl.h(14537): error C2061: syntax error: identifier '__RPC__in_xcount'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidl.h(14541): error C2061: syntax error: identifier '__RPC__inout_xcount'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidl.h(14542): error C2061: syntax error: identifier '__RPC__in_xcount'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidl.h(14546): error C2061: syntax error: identifier '__RPC__inout_xcount'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidl.h(14547): error C2061: syntax error: identifier '__RPC__in_xcount'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidl.h(14551): error C2061: syntax error: identifier '__RPC__inout_xcount'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidl.h(14552): error C2061: syntax error: identifier '__RPC__in_xcount'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidl.h(14556): error C2061: syntax error: identifier '__RPC__inout_xcount'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objidl.h(14557): error C2061: syntax error: identifier '__RPC__in_xcount'

And many more...

I thought this would happen because the DirectX includes override something when they are loaded in before and/or are prioritized over the windows headers, so i tried replacing the line with one of these:

include_directories(BEFORE "C:/Program Files (x86)/Microsoft DirectX SDK (March 2008)/Include")
include_directories(AFTER "C:/Program Files (x86)/Microsoft DirectX SDK (March 2008)/Include")
include_directories(BEFORE SYSTEM "C:/Program Files (x86)/Microsoft DirectX SDK (March 2008)/Include")
include_directories(AFTER SYSTEM "C:/Program Files (x86)/Microsoft DirectX SDK (March 2008)/Include")

but nothing changed. I also tried including windows.h after some directX headers like d3d11.h d3dcompiler.h d3dcommon.h etc. hoping it would do something, but as expected nothing changed. I would really appreciate some help, because i really don't know how i'm supposed to fix this...


Solution

  • The problem is that the legacy DirectX SDK contains a number of headers that are outdated, and conflict with the Windows SDK. In this case, when you included Windows.h it came from the Windows SDK, but then when that header looked for rcpsal.h it pulled the ancient copy out of the legacy DirectX SDK.

    Header/library include order helps if you put the DXSDK at the end, but an even better solution is to (a) not use the legacy DirectX SDK at all per this blog post, or (b) if you must use it, trim down per this blog post.

    See also Where is the DirectX SDK (2021 Edition)?.

    The VCPkg version of the directxsdk port supports the 'trimmed-down' version. See this PR.

    UPDATE: If you want to make use of legacy D3DX9, D3DX10, or D3DX11 for some reason, you can make use of either this NuGet package or this vcpkg port. This works much better than trying to use the legacy DirectX SDK for anything.

    If you need XAudio2 for Windows 7, use XAudio2Redist via NuGet or vcpkg.

    Avoid using XInput 1.3 as it not needed for most cases. For Windows 7 you can just use the built in XInput 9.1.0.