首页 > 解决方案 > 无法使用构建后命令将 .dll 移动到可执行文件

问题描述

所以我对大型项目的工作相当陌生。我正在尝试使用 premake5 为 Visual Studio 2019 生成项目和解决方案

这就是我的预制 lua 文件中的内容

    workspace "DragonHeart"
    architecture "x64"
    startproject "Sandbox"
    
    configurations
    {
        "Debug",
        "Release",
        "Dist"
    }
    
outputdir = "%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}"
    
project "DragonHeart"
    location "DragonHeart"
    kind "SharedLib"
    language "C++"
    
    targetdir ("bin" .. outputdir .. "/%{prj.name}")
    objdir ("bin-int" .. outputdir .. "/%{prj.name}")
    
    files
    {
        "%{prj.name}/src/**.h",
        "%{prj.name}/src/**.cpp"
    }
    
    includedirs
    {
        "%{prj.name}/vendor/spdlog/include"
    }
    
    
    filter "system:windows"
        cppdialect "C++14"
        staticruntime "On"
        systemversion "latest"
        
        defines
        {
            "DH_PLATFORM_WINDOWS",
            "DH_BUILD_DLL"
        }
        
        postbuildcommands
        {
            {"{COPY} %{cfg.buildtarget.relpath} ../bin/" .. outputdir .. "/Sandbox"}
        }
        
    filter "configurations:Debug"
        defines "DH_DEBUG"
        symbols "On"
    
    filter "configurations:Release"
        defines "DH_RELEASE"
        optimize "On"
        
    filter "configurations:Dist"
        defines "DH_DIST"
        optimize "On"
        
project "Sandbox"
    
    location "Sandbox"
    kind "ConsoleApp"
    language "C++"
    
    targetdir ("bin" .. outputdir .. "/%{prj.name}")
    objdir ("bin-int" .. outputdir .. "/%{prj.name}")
    
    files
    {
        "%{prj.name}/src/**.h",
        "%{prj.name}/src/**.cpp"
    }
    
    includedirs
    {
        "DragonHeart/vendor/spdlog/include",
        "DragonHeart/src"
    }
    
    links
    {
        "DragonHeart"
    }
    
    filter "system:windows"
        cppdialect "C++14"
        staticruntime "On"
        systemversion "latest"
        
        defines
        {
            "DH_PLATFORM_WINDOWS"
        }
        
        
    filter "configurations:Debug"
        defines "DH_DEBUG"
        symbols "On"
    
    filter "configurations:Release"
        defines "DH_RELEASE"
        optimize "On"
        
    filter "configurations:Dist"
        defines "DH_DIST"
        optimize "On"

我的问题是当我运行并尝试构建东西时,VS2019 遇到了后期构建的问题。

Severity    Code    Description Project File    Line    Suppression State
Error   MSB3073 The command "IF EXIST ..\binDebug-windows-x86_64\DragonHeart\DragonHeart.dll\ (xcopy /Q /E /Y /I ..\binDebug-windows-x86_64\DragonHeart\DragonHeart.dll ..\bin\Debug-windows-x86_64\Sandbox > nul) ELSE (xcopy /Q /Y /I ..\binDebug-windows-x86_64\DragonHeart\DragonHeart.dll ..\bin\Debug-windows-x86_64\Sandbox > nul)
:VCEnd" exited with code 2. DragonHeart C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 155 

我不确定为什么它不起作用。我知道 .dll 之后有一个 \ 但手动删除它不会改变任何东西。而且我认为退出代码2意味着它找不到文件,但是将文件路径与实际存在的路径进行比较,它看起来应该找到它。我不知道如何解决这个问题,任何帮助将不胜感激。

标签: c++lua

解决方案


我遇到了和你一样的问题,我通过将 postbuildcommands 放在 Sandbox 项目中并更改复制指令的目录来解决它。您应该在 Sandbox 项目的 windows 过滤器中添加它,并从 DragonHeart 项目中删除 postbuildcommands

    postbuildcommands
    {
        "{COPY} ../bin/" .. outputdir .. "/DragonHeart/*.dll ../bin/" .. outputdir .. "/Sandbox"
    }

我不知道为什么其他代码不起作用,但这似乎绕过了问题


推荐阅读