visual-studio 如何让 Visual Studio 将 DLL 文件复制到输出目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1776060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:14:32  来源:igfitidea点击:

How to make Visual Studio copy a DLL file to the output directory?

visual-studiovisual-c++dll

提问by Mat

I have a Visual Studio C++ project that relies on an external DLL file. How can I make Visual Studio copy this DLL file automatically into the output directory (debug/release) when I build the project?

我有一个依赖于外部 DLL 文件的 Visual Studio C++ 项目。生成项目时,如何让 Visual Studio 自动将此 DLL 文件复制到输出目录(调试/发布)中?

采纳答案by Adrien Plisson

Use a post-build action in your project, and add the commands to copy the offending DLL. The post-build action are written as a batch script.

在您的项目中使用构建后操作,并添加命令以复制有问题的 DLL。构建后操作被编写为批处理脚本。

The output directory can be referenced as $(OutDir). The project directory is available as $(ProjDir). Try to use relative pathes where applicable, so that you can copy or move your project folder without breaking the post-build action.

输出目录可以引用为$(OutDir). 项目目录可作为$(ProjDir). 尝试在适用的情况下使用相对路径,以便您可以复制或移动项目文件夹而不会中断构建后操作。

回答by Nesho Neshev

$(OutDir) turned out to be a relative path in VS2013, so I had to combine it with $(ProjectDir) to achieve the desired effect:

$(OutDir)在VS2013中竟然是一个相对路径,所以我不得不把它和$(ProjectDir)结合起来才能达到预期的效果:

xcopy /y /d  "$(ProjectDir)External\*.dll" "$(ProjectDir)$(OutDir)"

BTW, you can easily debug the scripts by adding 'echo ' at the beginning and observe the expanded text in the build output window.

顺便说一句,您可以通过在开头添加“echo”并观察构建输出窗口中的扩展文本来轻松调试脚本。

回答by SCBuergel.eth

The details in the comments section above did not work for me (VS 2013) when trying to copy the output dll from one C++ project to the release and debug folder of another C# project within the same solution.

当尝试将输出 dll 从一个 C++ 项目复制到同一解决方案中另一个 C# 项目的发布和调试文件夹时,上面评论部分中的详细信息对我(VS 2013)不起作用。

I had to add the following post build-action (right click on the project that has a .dll output) then properties -> configuration properties -> build events -> post-build event -> command line

我必须添加以下构建后操作(右键单击具有 .dll 输出的项目)然后属性 -> 配置属性 -> 构建事件 -> 构建后事件 -> 命令行

now I added these two lines to copy the output dll into the two folders:

现在我添加了这两行来将输出的 dll 复制到两个文件夹中:

xcopy /y $(TargetPath) $(SolutionDir)aeiscontroller\bin\Release
xcopy /y $(TargetPath) $(SolutionDir)aeiscontroller\bin\Debug

回答by Mark Lakata

(This answer only applies to C# not C++, sorry I misread the original question)

(此答案仅适用于 C# 不适用于 C++,抱歉我误读了原始问题)

I've got through DLL hell like this before. My final solution was to store the unmanaged DLLs in the managed DLL as binary resources, and extract them to a temporary folder when the program launches and delete them when it gets disposed.

我以前也经历过这样的 DLL 地狱。我的最终解决方案是将非托管 DLL 作为二进制资源存储在托管 DLL 中,并在程序启动时将它们提取到一个临时文件夹中,并在处理时删除它们。

This shouldbe part of the .NET or pinvoke infrastructure, since it is so useful.... It makes your managed DLL easy to manage, both using Xcopy or as a Project reference in a bigger Visual Studio solution. Once you do this, you don't have to worry about post-build events.

应该是 .NET 或 pinvoke 基础结构的一部分,因为它非常有用......它使您的托管 DLL 易于管理,无论是使用 Xcopy 还是作为更大的 Visual Studio 解决方案中的项目引用。完成此操作后,您就不必担心构建后事件。

UPDATE:

更新:

I posted code here in another answer https://stackoverflow.com/a/11038376/364818

我在另一个答案中发布了代码https://stackoverflow.com/a/11038376/364818

回答by John_J

Add builtin COPY in project.csprojfile:

project.csproj文件中添加内置 COPY :

  <Project>
    ...
    <Target Name="AfterBuild">
      <Copy SourceFiles="$(ProjectDir)..\..\Lib\*.dll" DestinationFolder="$(OutDir)Debug\bin" SkipUnchangedFiles="false" />
      <Copy SourceFiles="$(ProjectDir)..\..\Lib\*.dll" DestinationFolder="$(OutDir)Release\bin" SkipUnchangedFiles="false" />
    </Target>
  </Project>

回答by Rich Shealer

xcopy /y /d  "$(ProjectDir)External\*.dll" "$(TargetDir)"

You can also refer to a relative path, the next example will find the DLL in a folder located one level above the project folder. If you have multiple projects that use the DLL in a single solution, this places the source of the DLL in a common area reachable when you set any of them as the Startup Project.

您也可以引用相对路径,下一个示例将在位于项目文件夹上一级的文件夹中查找 DLL。如果您有多个项目在单个解决方案中使用 DLL,则当您将它们中的任何一个设置为启动项目时,这会将 DLL 的源放置在可访问的公共区域中。

xcopy /y /d  "$(ProjectDir)..\External\*.dll" "$(TargetDir)"

The /yoption copies without confirmation. The /doption checks to see if a file exists in the target and if it does only copies if the source has a newer timestamp than the target.

/y选项无需确认即可复制。该/d选项检查目标中是否存在文件,以及是否仅在源具有比目标更新的时间戳时才进行复制。

I found that in at least newer versions of Visual Studio, such as VS2109, $(ProjDir)is undefined and had to use $(ProjectDir)instead.

我发现至少在较新版本的 Visual Studio 中,例如 VS2109,$(ProjDir)是未定义的,必须$(ProjectDir)改用。

Leaving out a target folder in xcopyshould default to the output directory. That is important to understand reason $(OutDir)alone is not helpful.

省略目标文件夹xcopy应默认为输出目录。这很重要,$(OutDir)仅理解原因是没有帮助的。

$(OutDir), at least in recent versions of Visual Studio, is defined as a relative path to the output folder, such as bin/x86/Debug. Using it alone as the target will create a new set of folders starting from the project output folder. Ex: … bin/x86/Debug/bin/x86/Debug.

$(OutDir),至少在最近版本的 Visual Studio 中,被定义为输出文件夹的相对路径,例如bin/x86/Debug. 单独使用它作为目标将从项目输出文件夹开始创建一组新文件夹。例如:… bin/x86/Debug/bin/x86/Debug

Combining it with the project folder should get you to the proper place. Ex: $(ProjectDir)$(OutDir).

将它与项目文件夹结合起来应该会让你到正确的地方。例如:$(ProjectDir)$(OutDir)

However $(TargetDir)will provide the output directory in one step.

但是$(TargetDir)将一步提供输出目录。

Microsoft's list of MSBuild macros for current and previous versions of Visual Studio

Microsoft 的当前和以前版本的 Visual Studio 的 MSBuild 宏列表