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

前端 未结 6 935
广开言路
广开言路 2020-11-27 12:47

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/releas

相关标签:
6条回答
  • 2020-11-27 13:20
    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.

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

    The /y option copies without confirmation. The /d option 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.

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

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

    $(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.

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

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

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

    0 讨论(0)
  • 2020-11-27 13:23

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

    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.

    0 讨论(0)
  • 2020-11-27 13:30

    Add builtin COPY in project.csproj file:

      <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>
    
    0 讨论(0)
  • 2020-11-27 13:31

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

    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.

    This should be 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.

    UPDATE:

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

    0 讨论(0)
  • 2020-11-27 13:37

    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.

    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

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

    xcopy /y $(TargetPath) $(SolutionDir)aeiscontroller\bin\Release
    xcopy /y $(TargetPath) $(SolutionDir)aeiscontroller\bin\Debug
    
    0 讨论(0)
  • 2020-11-27 13:38

    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.

    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.

    0 讨论(0)
提交回复
热议问题