4

So I've been trying to learn CMake and use it with C++. I'd like to have a go at creating a portable game engine which uses Direct3D 12 on Windows.

Currently, I have the following CMakeLists.txt for my project:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

project(PsychoEngineCore)

set(SRCS_CXX ${CMAKE_CURRENT_LIST_DIR})

include(${CMAKE_CURRENT_SOURCE_DIR}/src/dir_src.cmake)

set(LIB_TYPE "STATIC" CACHE STRING "Static or Dynamic Linking")

if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU OR
${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
    set(warnings "-Wall -Wextra -Werror")
elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
    set(warnings "/W4 /WX /EHsc")
endif()

if(NOT CONFIGURED_ONCE)
    set(CMAKE_CXX_FLAGS "${warnings}"
        CACHE STRING "Flags used by the compiler during all build types." FORCE)
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

# Do we want static libraries?
# When STATIC_LINKING is TRUE, then cmake looks for binaries ending in ".a".
# THIS IS FOR LINUX ONLY!
if(LIB_TYPE EQUAL STATIC)
    if (UNIX AND NOT APPLE)
        set(CMAKE_FIND_LIBRARY_SUFFIXES(".a"))
    endif(UNIX AND NOT APPLE)

    set(CMAKE_EXE_LINKER_FLAGS "-static")
    set_target_properties(surface PROPERTIES LINK_SEARCH_END_STATIC 1)
endif(LIB_TYPE EQUAL STATIC)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include(GenerateExportHeader)
add_library(${PROJECT_NAME} ${LIB_TYPE} ${SRCS_CXX})
GENERATE_EXPORT_HEADER(
    ${PROJECT_NAME}
    EXPORT_MACRO_NAME PE_API
    EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/include/Engine/API/${PROJECT_NAME}Export.hpp
    DEPRECATED_MACRO_NAME PE_API_DEP
    STATIC_DEFINE PE_STATIC
)

Currently I have my d3d12.lib file in the following location:

C:\Program Files (x86)\Windows Kits\10\Libs\10.0.16299.0\um\x64

Is there a way to keep the path dynamic and preferably "update" if a newer version is available on said system?

Thanks!

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
JamieRhys
  • 206
  • 6
  • 24
  • Can you please give a [mcve] of where the library is currently embedded in your code? The shown example doesn't seem to reference the Direct3D library. Generally speaking, just add a `target_link_libraries((${PROJECT_NAME} d3d12.lib)` call. The Windows SDK (where [the Direct3D SDK is now part of](https://msdn.microsoft.com/en-us/library/windows/desktop/ee663275(v=vs.85).aspx)) is in the standard search paths of the linker for libraries. And I don't think that upgrading to a newer (yet unknown) API version automatically is a good idea. Windows does also support "older" API versions. – Florian Feb 23 '18 at 21:11
  • @Florian So, so long as the **Direct3D SDK** is within my filepath (Which as you say is standard anyway) Then CMake should in theory find what ever version is present on the compilers PC? – JamieRhys Feb 24 '18 at 23:33

1 Answers1

3

Turning my comment into an answer

Generally speaking, just add a

target_link_libraries((${PROJECT_NAME} d3d12.lib) 

The Windows SDK (where the Direct3D SDK is now part of) is in the standard search paths e.g. of the linker for libraries. So it's found automatically by the MSVC compiler and linker.

And I don't think that upgrading to a newer (yet unknown) API version automatically is a good idea. You're writing your program for a specific API version.

Working Example

Here is a minimal working example (Tested VS2017 15.5.5, CMake 3.9.0):

cmake_minimum_required(VERSION 3.0)

project(Direct3DExample)

find_package(Git REQUIRED)

set(_path "${CMAKE_BINARY_DIR}/DirectX-Graphics-Samples/Samples/Desktop/D3D12HelloWorld/src/HelloTriangle")
if (NOT EXISTS "${_path}")
    execute_process(
        COMMAND "${GIT_EXECUTABLE}" clone https://github.com/Microsoft/DirectX-Graphics-Samples.git
    )
endif()

file(GLOB _files "${_path}/*")
list(APPEND _shader ${_files})
list(FILTER _files EXCLUDE REGEX "\\.vcxproj|\\.hlsl")
list(FILTER _shader INCLUDE REGEX "\\.hlsl")

get_filename_component(_name "${_path}" NAME)

add_executable(${_name} WIN32 ${_files})
target_compile_definitions(${_name} PRIVATE "UNICODE" "_UNICODE")
target_link_libraries(${_name} PRIVATE "d3d12.lib" "dxgi.lib" "d3dcompiler.lib")

add_custom_command(
    TARGET ${_name}
    POST_BUILD
    COMMAND "${CMAKE_COMMAND}" -E copy ${_shader} $<TARGET_FILE_DIR:${_name}>
)

If you need more (compilers/linkers that are not finding the Windows SDK automatically), you may want to look the following find_package() config code:

Florian
  • 39,996
  • 9
  • 133
  • 149
  • I changed the line set(_path "${CMAKE_BINARY_DIR}/Dire... to set(_path "${CMAKE_SOURCE_DIR}/Dire.., and it works. GOOD JOB Florian! – learner Oct 10 '22 at 03:59
  • Ah, the `d3dcompiler` was really helpful. I was trying to compile some HLSL shaders and kept getting an undefined reference until I linked that library. – rbaleksandar Oct 17 '22 at 10:04