我很难弄清楚DirectX 11(特别是我对Direct3D感兴趣(如何在Windows 10上与CMake集成。
在C:WindowsSystem32
中可以找到运行库(DLL(,因为现在DirectX已集成到Windows 10的安装中(对我来说,重要的是d3d11.dll
和d3dx11.dll
(。然而,DLL实际上并不足以开发使用DirectX的代码。
为了在Windows 10上为Windows 10开发软件,通常需要Windows SDK。我有10和8.1。DirectX 9、10、11和12相关文件可以在C:Program Files (x86)Windows Kits10Lib10.0.17763.0umx86
或...x64
中找到,具体取决于您想要32位还是64位。在C:Program Files (x86)Windows Kits10Include10.0.17763.0um
中可以找到头文件。
人们说简单地做
target_link_libraries(my_project PRIVATE d3d11 d3dx11)
应该做到这一点。然而,我得到了一个错误(在Visual Studio 2017 Pro中使用CMake 3.12,前者作为后者的一部分提供(,即找不到.lib
。
我尝试添加以下
link_directories("C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\um\x64")
include_directories("C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um")
但一切都没有改变。
以下是我的简单项目的完整CMakeLists.txt
cmake_minimum_required (VERSION 3.12)
link_directories("C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\um\x64")
include_directories("C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um")
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
message(STATUS "Include directory: "${dir}"")
endforeach()
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY LINK_DIRECTORIES)
foreach(dir ${dirs})
message(STATUS "Link directory: "${dir}"")
endforeach()
add_executable (DirectX11_CMake_Example "DirectX11_CMake_Example.cpp" "DirectX11_CMake_Example.h")
target_compile_definitions(DirectX11_CMake_Example PRIVATE "UNICODE" "_UNICODE")
target_link_libraries(DirectX11_CMake_Example PRIVATE d3d11 d3dx11)
我在GitHub上为DirectX 11和DirectX 12提供了基本的"游戏循环",包括窗口、设备和交换链,包括"经典"Win32项目和UWP应用程序。存储库主要为VSIX构建Visual Studio模板,但我也提供了一个简单的PowerShell脚本和CMakeLists.txt文件来实例化CMake的模板。请参阅wiki。
您的场景得到的CMakeLists.txt
是:
cmake_minimum_required (VERSION 3.13)
project(CMakeDirect3DGame
DESCRIPTION "CMake example for Direct3D 11 Game (Win32)"
LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/CMake")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/CMake")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/CMake")
set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO Release)
if (DEFINED VCPKG_TARGET_ARCHITECTURE)
set(DIRECTX_ARCH ${VCPKG_TARGET_ARCHITECTURE})
elseif(CMAKE_GENERATOR_PLATFORM MATCHES "^[Ww][Ii][Nn]32$")
set(DIRECTX_ARCH x86)
elseif(CMAKE_GENERATOR_PLATFORM MATCHES "^[Xx]64$")
set(DIRECTX_ARCH x64)
elseif(CMAKE_GENERATOR_PLATFORM MATCHES "^[Aa][Rr][Mm]$")
set(DIRECTX_ARCH arm)
elseif(CMAKE_GENERATOR_PLATFORM MATCHES "^[Aa][Rr][Mm]64$")
set(DIRECTX_ARCH arm64)
endif()
add_executable(${PROJECT_NAME} WIN32
Game.cpp
Game.h
Main.cpp
StepTimer.h
pch.h
)
if ((${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16") AND (NOT MINGW))
target_precompile_headers(${PROJECT_NAME} PRIVATE pch.h )
endif()
target_link_libraries(${PROJECT_NAME} PRIVATE
d3d11.lib dxgi.lib dxguid.lib uuid.lib
kernel32.lib user32.lib
comdlg32.lib advapi32.lib shell32.lib
ole32.lib oleaut32.lib
)
if(MSVC)
# Use max Warning Level
string(REPLACE "/W3 " "/Wall " CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
string(REPLACE "/W3 " "/Wall " CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
string(REPLACE "/W3 " "/Wall " CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
target_compile_options(${PROJECT_NAME} PRIVATE /fp:fast "$<$<NOT:$<CONFIG:DEBUG>>:/guard:cf>")
target_link_options(${PROJECT_NAME} PRIVATE "$<$<NOT:$<CONFIG:DEBUG>>:/guard:cf>" /DYNAMICBASE /NXCOMPAT)
if((${CMAKE_SIZEOF_VOID_P} EQUAL 4) AND (NOT ${DIRECTX_ARCH} MATCHES "^arm"))
target_link_options(${PROJECT_NAME} PRIVATE /SAFESEH)
endif()
if((MSVC_VERSION GREATER_EQUAL 1928) AND (CMAKE_SIZEOF_VOID_P EQUAL 8)
AND ((NOT (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) OR (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13.0)))
target_compile_options(${PROJECT_NAME} PRIVATE "$<$<NOT:$<CONFIG:DEBUG>>:/guard:ehcont>")
target_link_options(${PROJECT_NAME} PRIVATE "$<$<NOT:$<CONFIG:DEBUG>>:/guard:ehcont>")
endif()
endif()
if(NOT ${DIRECTX_ARCH} MATCHES "^arm")
if (${CMAKE_SIZEOF_VOID_P} EQUAL "4")
set(ARCH_SSE2 $<$<CXX_COMPILER_ID:MSVC>:/arch:SSE2> $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-msse2>)
else()
set(ARCH_SSE2 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-msse2>)
endif()
target_compile_options(${PROJECT_NAME} PRIVATE ${ARCH_SSE2})
endif()
if (MINGW OR VCPKG_TOOLCHAIN)
message("INFO: Using VCPKG for DirectXMath.")
find_package(directxmath CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE Microsoft::DirectXMath)
# Uncomment if using the DirectX Tool Kit
# find_package(directxtk CONFIG REQUIRED)
# target_link_libraries(${PROJECT_NAME} PRIVATE Microsoft::DirectXTK)
endif()
if ( CMAKE_CXX_COMPILER_ID MATCHES "Clang|IntelLLVM" )
target_compile_options(${PROJECT_NAME} PRIVATE
-Wpedantic -Wextra
"-Wno-c++98-compat" "-Wno-c++98-compat-pedantic"
"-Wno-exit-time-destructors" "-Wno-global-constructors" "-Wno-language-extension-token"
"-Wno-missing-prototypes" "-Wno-missing-variable-declarations" "-Wno-reserved-id-macro")
if(BUILD_TEST_TEMPLATE)
target_compile_options(${PROJECT_NAME} PRIVATE "-Wno-unused-value")
endif()
endif()
if(MINGW)
target_compile_options(${PROJECT_NAME} PRIVATE -Wno-ignored-attributes)
target_link_options(${PROJECT_NAME} PRIVATE -municode)
endif()
if ( CMAKE_CXX_COMPILER_ID MATCHES "MSVC" )
target_compile_options(${PROJECT_NAME} PRIVATE
/sdl /permissive- /Zc:__cplusplus
"/wd4061" "/wd4365" "/wd4514" "/wd4571" "/wd4668" "/wd4710" "/wd4820" "/wd5039" "/wd5045")
if(ENABLE_CODE_ANALYSIS)
target_compile_options(${PROJECT_NAME} PRIVATE /analyze)
endif()
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.24)
target_compile_options(${PROJECT_NAME} PRIVATE /ZH:SHA_256)
endif()
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.26)
target_compile_options(${PROJECT_NAME} PRIVATE /Zc:preprocessor /wd5105)
endif()
if ((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.27) AND (NOT (${DIRECTX_ARCH} MATCHES "^arm")))
target_link_options(${PROJECT_NAME} PRIVATE /CETCOMPAT)
endif()
if(BUILD_TEST_TEMPLATE)
target_compile_options(${PROJECT_NAME} PRIVATE "/wd4555")
endif()
endif()
if ( CMAKE_CXX_COMPILER_ID MATCHES "^Intel$" )
target_compile_options(${PROJECT_NAME} PRIVATE /Qwd161)
endif()
if(WIN32)
target_compile_definitions(${PROJECT_NAME} PRIVATE _UNICODE UNICODE)
if (${DIRECTX_ARCH} MATCHES "^arm")
target_compile_definitions(${PROJECT_NAME} PRIVATE _WIN32_WINNT=0x0A00)
else()
target_compile_definitions(${PROJECT_NAME} PRIVATE _WIN32_WINNT=0x0601)
endif()
endif()
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME})
D3DX11
实用程序库已弃用,并且仅作为旧版DirectX SDK的一部分提供。对于新项目,你应该根据这篇博客文章避免它也就是说,如果你真的想将它与CMake一起使用,你可以使用vcpkg和dxsdk-d3dx端口。