DirectX12 与 Premake5:链接 Directx12 静态库

DirectX12 with Premake5: Linking Directx12 Static Libraries

我正在切换到使用 premake5 而不是直接使用 Visual Studio 2017。

这是我过去如何链接 Dx12 的。 我会把这些宏放在我的 main.cpp 中,效果很好。

    #pragma comment(lib, "d3d12.lib")
    #pragma comment(lib, "dxgi.lib")
    #pragma comment(lib, "d3dcompiler.lib")

但是,我被建议不要在我的源代码中包含库。当我将我的项目转换为 premake5 时,我想知道处理这种情况的正确方法。

抱歉,我是 premake5 等工具的新手。我不确定如何进行。

更新一: 我尝试添加以下代码来解决链接器错误。

    print("Linking DX12 Libs") 
    libdirs { 
    os.findlib("d3d12.lib"), 
    os.findlib("dxgi.lib"), 
    os.findlib("d3dcompiler.lib") } 
    links { "d3d12.lib", "dxgi.lib", "d3dcompiler.lib" }

但是,我仍然遇到链接器错误。

对于问题的第一部分,您需要做的就是在脚本中使用 links。它看起来像这样:

links
{
   "d3d12.lib",
   "dxgi.lib",
   "d3dcompiler.lib"
}

如果 lib 文件位于根目录(构建解决方案的位置),则以上方法有效。如果它们在其他文件夹中,您可以使用 Tokens,例如 %{prj.location}%{prj.name}

DLPDev 大部分*是正确的。

*When specifying libraries, system-specific decorations, such as prefixes or file extensions, should be omitted. Premake will synthesize the correct format based on the target platform automatically. The one exception to the rule is Mac OS X frameworks, where the file extension is required to identify it as such.

由于对过滤功能的无知,我犯了一个严重的错误。在发布过滤器之后调用 links 之前。仅在发布模式下链接 dx12 库。

   -- This is all you need to link against dx12 there is no special sauce
   -- You don't need to call libdirs or os.findlib for the dx12 libraries
   -- This code works for both configurations since it is called before the filter function
   links { "d3d12", "dxgi", "d3dcompiler" }
   filter("configurations:Debug")
      defines({ "DEBUG" })
      symbols("On")
      optimize("Off")
   filter("configurations:Release")
      defines({ "NDEBUG" })
      symbols("On")`

TLDR:使用 links 函数时注意不要包含文件扩展名。并注意 filter 函数

的范围